Hibernate cascade save parent/child with sequenced FK

天涯浪子 提交于 2019-12-10 11:46:19

问题


I have read many posts on this topic but have not been able to solve it =(

I'm trying to save a parent and its children that have a one-to-many relationship. I believe I'm following the documentation and other suggestions from other posts. However, the problem I'm having is that when Hibernate tries to save the child records, it's inserting '0' for the FK to the parent, instead of the real id.

The parent hbm.xml mapping

<hibernate-mapping>
<class name="ParentClass" table="PARENTTABLE">
    <id name="parentid" type="java.lang.Long">
        <column name="PARENTID" precision="15" scale="0" />
        <generator class="sequence">
            <param name="sequence">S_PARENTTABLE</param>
        </generator>
    </id>
...
    <set name="children" table="CHILDTABLE" inverse="true" lazy="true" fetch="select" cascade="all">
        <key>
            <column name="PARENTID" precision="15" scale="0" not-null="true" />
        </key>
        <one-to-many class="ParentClass" />
    </set>
...
</class>
</hibernate-mapping>

The child hbm.xml mapping

<hibernate-mapping>
<class name="ChildClass" table="CHILDTABLE">
    <composite-id name="id" class="ChildClassId">
        ....
        <key-property name="parentid" type="long">
            <column name="PARENTID" precision="15" scale="0" />
        </key-property>
    </composite-id>
    <many-to-one name="parent" class="ParentClass" update="false" insert="false" fetch="select">
        <column name="PARENTID" precision="15" scale="0" not-null="true" />
    </many-to-one>
....
</class>
</hibernate-mapping>

Java code to save Objects

private myMethod(ParentClass p, HashSet<ChildClass> children){
    for(ChildClass child : children){
        child.setParent(p);
    }
    p.setChildren(children);  //The parameter type is Set<ChildClass>   
    sess.save(p);
    ...
}

So it will save the Parent object with the correct id (the next seq value, say 273) but when it goes and tries to save the child objects, instead of using 273, it's just using 0.

I can't figure how to get Hibernate to save the child objects with the correct ParentId (FK).

Any help you can offer or any ideas would be very much appreciate! Thanks in advance!

** UPDATE ** I finally figured out why the FK in the child table was not being inserted with the correct value. It was due to the mapping definition. I had run a reverse engineer on my DB and I checked a box that said 'Generate basic typed composite IDs'. This causes Hibernate to map composite key columns as primitive types instead of references to an entity. This was somehow confusing it when I did the inserts.

The correct child table mapping should look more like this:

Updated child hbm.xml mapping

<hibernate-mapping>
<class name="ChildClass" table="CHILDTABLE">
    <composite-id name="id" class="ChildClassId">
        ....
        <key-many-to-one name="parentid" class="ParentClass" >
            <column name="PARENTID" precision="15" scale="0" />
        </key-many-to-one>
    </composite-id>
</class>
</hibernate-mapping>

Notice the tag instead of the tag from before. Also, the tag outside of the is gone (or rather moved into the id as a tag).


回答1:


I think that in your case you shoud set inverse property to false in one-to-many mapping definition for parent class. This means that your parent entity will be responsible for updating FK in child table. In such case hiberntewill execute additional update statement for updating FK in child table after saving parent entity.

<set name="children" inverse="false" lazy="true" fetch="select" cascade="all">
       <key>
           <column name="PARENTID" precision="15" scale="0" not-null="true" />
       </key>
       <one-to-many class="ParentClass" />
</set>


来源:https://stackoverflow.com/questions/11961071/hibernate-cascade-save-parent-child-with-sequenced-fk

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