开发者

JPA2 and hibernate - why does merge store child entities whilst persist does not?

开发者 https://www.devze.com 2023-04-09 04:37 出处:网络
I have the usual parent - child OneToMany relationship: @OneToMany(mappedBy = \"mapType\", cascade = CascadeType.ALL, orphanRemoval = true)

I have the usual parent - child OneToMany relationship:

@OneToMany(mappedBy = "mapType", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Child> getChildren() {
    return c开发者_开发百科hildren;
}

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 annotation fetch = FetchType.EAGER. Default value is LAZY. It'll be helpful because hibernate will be manage collection permanently then you make changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消