Hibernate:级联问题
在hibernate中,有很多关于set Cascade to “all,delete”等的信息, 但我想知道设置级联为“无”的效果
现在我有一个类Parent,它是子类Child,
class Parent{
List<Child> childs;
....}
并且在文件parent.hbm.xml中(我省略了其他内容)
<class name="Parent" table="parent" >
<bag name="childs" lazy="false" table="parenthaschildsTable" cascade="none">
<key>
<column name="parentId" not-null="true"/>
</key>
<one-to-many class="Child">
<column name="childId" not-null="true"/>
</one-to-many>
</bag>
当保存父级时,我不想级联更新他的孩子,所以我设置了 cascade="none"
。 我的问题是: 我设置级联为“无”,如果我向父级添加子级#1,然后保存父级,hibernate可以将新记录插入到表parenthaschildsTable中,但不级联子级吗?
In hibernate, there are many information about set cascade to "all, delete" and so on,
but I want to know the effect of set cascade to "none"
now I have a class Parent, and it's child-class Child,
class Parent{
List<Child> childs;
....}
and in the file parent.hbm.xml(I omitted other content)
<class name="Parent" table="parent" >
<bag name="childs" lazy="false" table="parenthaschildsTable" cascade="none">
<key>
<column name="parentId" not-null="true"/>
</key>
<one-to-many class="Child">
<column name="childId" not-null="true"/>
</one-to-many>
</bag>
when save the parent, I don't want to cascade update his childs, so I set cascade="none"
.
my question is :
I set the cascade is "none", if I add a child#1 to parent, then I save the parent, can hibernate insert a new record to the table parenthaschildsTable, but not cascade the Child?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您更改了父级(通过修改集合),因此 Hibernate 将在连接表中插入一条记录以反映保存时的情况(否则什么时候会发生?)。当然,只有当子级已经分配了标识符值时,这才会成功。但你为什么不实际尝试一下呢?
You changed the parent (by modifying a collection) so Hibernate will insert a record in the join table to reflect that on save (when should it happen else?). And of course this will only succeed if the child has already an identifier value assigned. But why don't you try it actually?