Hibernate级联使用排序的FK保存父/子 [英] Hibernate cascade save parent/child with sequenced FK

查看:158
本文介绍了Hibernate级联使用排序的FK保存父/子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读关于此主题的许多帖子,但尚未能解决它=($ / b
$ b

我试图挽救父母及其子女有一个一对多的关系,我相信我遵循其他帖子的文档和其他建议,但是我遇到的问题是,当Hibernate尝试保存子记录时,它将为FK插入'0'父母,而不是真正的ID。



父母hbm.xml映射



< pre $ < hibernate-mapping>
< class name =ParentClasstable =PARENTTABLE>
< id name =parentidtype = java.lang.Long>
< column name =PARENTIDprecision =15scale =0/>
< generator class =sequence>
< param name =sequence> S_PARENTTABLE< / param>
< / generator>
< / id>
...
< =childrentable =CHILDTABLEinverse =truelazy =truefetc h =selectcascade =all>
< key>
< column name =PARENTIDprecision =15scale =0not-null =true/>
< / key>
< / set>
...
< / class>
< / hibernate-mapping>

子hbm.xml映射

 < hibernate-mapping> 
< class name =ChildClasstable =CHILDTABLE>
< composite-id name =idclass =ChildClassId>
....
< key-property name =parentidtype =long>
< column name =PARENTIDprecision =15scale =0/>
< / key-property>
< / composite-id>
< column name =PARENTIDprecision =15scale =0not-null =true/>
< /多对一>
....
< / class>
< / hibernate-mapping>

保存对象的Java代码
$ (ChildClass child:children){
child.setParent(p))的私有myMethod(ParentClass p,HashSet< ChildClass> children){
> ;
}
p.setChildren(children); //参数类型是Set< ChildClass>
sess.save(p);
...
}

所以它会保存Parent对象正确的ID(下一个seq值,比如273),但是当它去并且试图保存子对象时,而不是使用273,它只是使用0。



我可以不知道如何让Hibernate用正确的ParentId(FK)保存子对象。

您可以提供的任何帮助或任何想法都将非常感谢!提前感谢!



** 更新 **
我终于明白为什么子表中的FK没有被插入具有正确的价值。这是由于映射定义。我在我的数据库上运行了一个反向工程师,并且选中了一个表示'生成基本类型化合成ID'的框。这导致Hibernate将复合键列映射为基本类型,而不是对实体的引用。这在我做插入操作时有点混乱。



正确的子表映射应该看起来更像这样:

更新后的hbm.xml映射

 < hibernate-mapping> 
< class name =ChildClasstable =CHILDTABLE>
< composite-id name =idclass =ChildClassId>
....
< column name =PARENTIDprecision =15scale =0/>
< / key-many-to-one>
< / composite-id>
< / class>
< / hibernate-mapping>

注意标签而不是之前的标签。此外,标签之外的标签已经不存在了(或者更确切地说,将标签作为标签移动到标识中)。

你的情况你应该在父类的一对多映射定义中设置inverse属性为false。这意味着您的父实体将负责更新子表中的FK。在这种情况下,hibernte将执行额外的更新语句,以便在保存父实体后更新子表中的FK。

 < set name =children inverse =falselazy =truefetch =selectcascade =all> 
< key>
< column name =PARENTIDprecision =15scale =0not-null =true/>
< / key>
< / set>


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

解决方案

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>

这篇关于Hibernate级联使用排序的FK保存父/子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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