简体   繁体   English

休眠-如何持久保存用公式映射的属性

[英]Hibernate - How to persist a property which is mapped with Formula

This question is very similar to the one asked here 这个问题和这里问的非常相似

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 ? 还有其他配置吗?

Query fired by hibernate : 休眠触发的查询:

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

Note how hibernate ignores the 'name' property in the insert query, which has a formula in the hbm. 请注意,hibernate如何忽略插入查询中的'name'属性,该查询在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 : 类:

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+ "]";
        }   


}

As per hibernate documentation it says: 根据休眠文档,它说:

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

formula (optional): an SQL expression that defines the value for a computed property. 公式(可选):一个SQL表达式,用于定义计算属性的值。 Computed properties do not have a column mapping of their own. 计算属性没有自己的列映射。

So the property with Formula cannot have a column mapping in DB table. 因此,具有Formula属性的属性不能在数据库表中具有列映射。 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. 作为替代方案,您可以具有另一个可以保存其值的属性,并且可以将此新属性映射到数据库表列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM