问题描述
这是我的 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)。
例如,如果向父实体实例中的 orderSetMembers
集合中添加 OrderSetMember
,但您离开在
字段 OrderSetMember
实例中的orderSet null
,然后调用 session.saveOrUpdate(orderSet)
,结果将是:
$ b
-
OrderSet
和OrderSetMember
实例保存到数据库(save
级联到子级)。 -
order_set_id
(映射到OrderSetMember
的表中的外键)是设置为null
。这是因为OrderSetMember
是关联的所有者,并且当Hibernate检查所有者时没有关联的OrderSet
实体
- 当您在新会话中阅读上述
OrderSet
实例时,您会注意到OrderSetMember
不在orderSetMembers
集合中。
This is my OrderSet.hbm
file. It has OrderSetMember
s 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:
- Both
OrderSet
andOrderSetMember
instances are saved to the database (save
is cascaded to the children). order_set_id
(foreign key in the table to whichOrderSetMember
is mapped) is set tonull
. This is becauseOrderSetMember
is the owner of the association, and there was no associatedOrderSet
entity when Hibernate inspected the owner of the association at dirty-check time.- When you read the above
OrderSet
instance in a new session, you'll notice that theOrderSetMember
is not present in theorderSetMembers
collection either.
这篇关于Cascade =“all-delete-orphan”逆= QUOT;真"一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!