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

≯℡__Kan透↙ 提交于 2019-12-11 09:28:34

问题


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.

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


}

回答1:


As per hibernate documentation it says:

5.1.4.2. Property mapping with hbm.xml

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.



来源:https://stackoverflow.com/questions/25772702/hibernate-how-to-persist-a-property-which-is-mapped-with-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!