JPA2和hibernate - 为什么合并存储的子实体,而坚持不存在? [英] JPA2 and hibernate - why does merge store child entities whilst persist does not?

查看:147
本文介绍了JPA2和hibernate - 为什么合并存储的子实体,而坚持不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    这篇关于JPA2和hibernate - 为什么合并存储的子实体,而坚持不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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