Hibernate - 如何保存与公式映射的属性 [英] Hibernate - How to persist a property which is mapped with Formula

查看:79
本文介绍了Hibernate - 如何保存与公式映射的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题非常类似于

This question is very similar to the one asked here

有人可以解释一下为什么hibernate决定忽略具有公式的属性,而完全保留。如果是这样,坚持一个有公式的财产的选择是什么?是否还有其他配置?

Could someone shed some light as why hibernate decides to ignore the property, which has a formula, completely while persisting. If so whats the alternative to persist a property which has a formula ? is there any additional config ?

hibernate查询:

Query fired by hibernate :

insert into fighterjet (max_speed, country, jet_id) values (?, ?, ?)

忽略插入查询中的'name'属性,该查询在hbm中有一个公式。

Note how hibernate ignores the 'name' property in the insert query, which has a formula in the hbm.

HBM:

HBM :

<hibernate-mapping>
    <class name="com.fighterjet.FighterjetDO" table="fighterjet">
        <id name="jetId" type="int" column="jet_id">
            <generator class="increment" />
        </id>
        <property name="name" formula="Select 'hi' from dual" >
            <column name="name"/>
        </property>
        <property name="maxSpeed">
            <column name="max_speed" />
        </property>
        <property name="country">
            <column name="country" />
        </property>
    </class>
</hibernate-mapping>

Class:

Class :

public class FighterjetDO {

    private Integer jetId;

    private String name;

    private Integer maxSpeed;

    private String country;

    public Integer getJetId() {
        return jetId;
    }

    public void setJetId(Integer jetId) {
        this.jetId = jetId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(Integer maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
        public String toString() {
            return "FighterJet [Jet_id=" + jetId + ", Name=" + name
                    + ", Max Speed=" + maxSpeed + ", Country=" + country+ "]";
        }   


}


推荐答案

根据 hibernate文档它说:

5.1.4.2。使用hbm.xml进行属性映射


公式(可选):一个SQL表达式,用于定义
计算属性。计算属性没有自己的
列映射。

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.

因此,具有公式的属性不能在数据库表中有列映射。作为替代方案,您可以拥有另一个可以保存其值的属性,并且可以将此新属性映射到您的数据库表列。

So the property with Formula cannot have a column mapping in DB table. As an alternative you can have another property that can hold its value and you can map this new property to your DB table column.

这篇关于Hibernate - 如何保存与公式映射的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆