Cascade =“all-delete-orphan”逆= QUOT;真"一起工作? [英] Does cascade="all-delete-orphan" inverse="true" work together?

查看:186
本文介绍了Cascade =“all-delete-orphan”逆= QUOT;真"一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 OrderSet.hbm 文件。它有 OrderSetMember s,因为它是小孩(一对多)关系。

 < list name =orderSetMemberslazy =truecascade =all-delete-orphaninverse =true> 
< key column =order_set_idnot-null =true/>
< list-index column =sequence_number/>
<一对多等级=OrderSetMember/>
< / list>

这是我的 OrderSetMember.hbm 文件。 OrderSetMember 与其父代具有多对一的关系。我想要一个双向映射。

 <多对一名称=orderSetclass =OrderSet> ; 
< column name =order_set_id/>
< /多对一>

父和子都可以用一个session-save命令保存吗?
或者我还需要保存另一个会话以保存孩子吗?

  Session session = sessionFactory.getCurrentSession(); 
session.saveOrUpdate(orderSet);

这些是我的数据模型:

  public class OrderSet {
private List< OrderSetMember> orderSetMembers;
}


公共类OrderSetMember {
private OrderSet orderSet;


解决方案

cascade inverse 有两个完全不同的用途。

级联告诉哪些实体生命周期操作应该自动应用于父级应用于子级(ren)。

/ code>表示该孩子是该关联的所有者。这进一步意味着,如果您不将孩子与孩子的父母关联起来,那么关系信息将不会与数据库同步。



例如,如果向父实体实例中的 orderSetMembers 集合中添加 OrderSetMember ,但您离开 OrderSetMember 实例中的orderSet 字段 null ,然后调用 session.saveOrUpdate(orderSet),结果将是:
$ b


  1. OrderSet OrderSetMember 实例保存到数据库( save 级联到子级)。

  2. order_set_id (映射到 OrderSetMember 的表中的外键)是设置为 null 。这是因为 OrderSetMember 是关联的所有者,并且当Hibernate检查所有者时没有关联的 OrderSet 实体
  3. 当您在新会话中阅读上述 OrderSet 实例时,您会注意到 OrderSetMember 不在 orderSetMembers 集合中。


This is my OrderSet.hbm file. It has OrderSetMembers as it's child (one-to-many) relationship.

<list name="orderSetMembers" lazy="true" cascade="all-delete-orphan" inverse="true">
    <key column="order_set_id" not-null="true"/>
    <list-index column="sequence_number"/>
    <one-to-many class="OrderSetMember" />
</list>

This is my OrderSetMember.hbm file. OrderSetMember has a many-to-one relationship with its parent. I wanted a bi-directional mapping.

<many-to-one name="orderSet" class="OrderSet">
    <column name="order_set_id"/>
</many-to-one>

Can the parent and the child both be saved with one session-save command? Or do I need to have another session save to save the child as well?

Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(orderSet);

These are my data-models:

public class OrderSet {
    private List<OrderSetMember> orderSetMembers;
}


public class OrderSetMember {
    private OrderSet orderSet;
}

解决方案

cascade and inverse have two completely different purposes.

cascade tells which entity lifecycle operation should be automatically applied on child(ren) when applied on parent.

inverse means that child is the owner of the association. That further means that if you don't associate child with the parent on the child side, the relationship information is not going to be synchronized with the database.

For example, if you add an OrderSetMember to the orderSetMembers collection in the parent entity instance, but you leave orderSet field null in the OrderSetMember instance, and then invoke session.saveOrUpdate(orderSet), the outcome will be:

  1. Both OrderSet and OrderSetMember instances are saved to the database (save is cascaded to the children).
  2. order_set_id (foreign key in the table to which OrderSetMember is mapped) is set to null. This is because OrderSetMember is the owner of the association, and there was no associated OrderSet entity when Hibernate inspected the owner of the association at dirty-check time.
  3. When you read the above OrderSet instance in a new session, you'll notice that the OrderSetMember is not present in the orderSetMembers collection either.

这篇关于Cascade =“all-delete-orphan”逆= QUOT;真&QUOT;一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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