JPA2和hibernate - 为什么合并存储的子实体,而坚持不存在? [英] JPA2 and hibernate - why does merge store child entities whilst persist does not?
问题描述
@OneToMany(mappedBy =mapType,cascade = CascadeType。 ALL,orphanRemoval = true)
public List< Child> getChildren(){
返回儿童;
}
我有相当标准的用例:
-
必须在persist上删除子项 - 此项工作正常。
- 通过添加采集。这对于已经坚持的父母来说工作得很好,但对于新父母来说不起作用。然而,EntityManager.merge 不会 添加新的孩子不适用于新的父对象?
我在Hibernate 3.6.6上。
@OneToMany 注释中添加此属性fetch = FetchType.EAGER
。默认值是LAZY。这将是有益的,因为休眠将永久性地管理集合,然后您进行更改。I have the usual parent - child OneToMany relationship:
@OneToMany(mappedBy = "mapType", cascade = CascadeType.ALL, orphanRemoval = true) public List<Child> getChildren() { return children; }
I have fairly standard use cases:
Must delete children on persist- this works fine.
Add new children by adding to collection. This works fine for already persisted parents, but does not work for new parents. EntityManager.merge however does persist the new parent with the new children.
Why would adding new children not work for new Parent objects? They're definitely there before persist is called.
I'm on Hibernate 3.6.6 by the way.
解决方案Try to add this attribute in
@OneToMany
annotationfetch = FetchType.EAGER
. Default value is LAZY. It'll be helpful because hibernate will be manage collection permanently then you make changes.这篇关于JPA2和hibernate - 为什么合并存储的子实体,而坚持不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文