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

ε祈祈猫儿з 提交于 2019-12-25 08:28:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7582589/jpa2-and-hibernate-why-does-merge-store-child-entities-whilst-persist-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!