简体   繁体   中英

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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