【问题标题】:Hibernate Collection is Null on updateHibernate Collection 在更新时为 Null
【发布时间】:2015-04-27 20:30:14
【问题描述】:

考虑以下几点:

@Entity
public class MainEntity {


    @OneToOne(orphanRemoval = true, cascade = CascadeType.ALL)
    private ChildEntity childEntity;
}

@Entity
public class ChildEntity {


    @OneToMany(cascade = CascadeType.ALL)
    @LazyCollection(FALSE)
    private List<AnotherEntity> otherEntities;
}

现在,当我第一次打电话时

final ChildEntity anewChild = new ChildEntity();
    anewChild.addOtherEntity(anotherEntity); //Several Entities can be added here
    mainEntity.setChildEntity(anewChild);
EntityManager.persist(mainEntity);

一切正常,然后我在事务完成很久之后进行一些更新。

final ChildEntity anotherNewChild = new ChildEntity();
anotherNewChild.addOtherEntity(anotherEntity); //Several Entities can be added here
mainEntity.setChildEntity(anotherNewChild);

//A log of LOG.info(mainEntity); shows all fields appropriately set
//At some point during merge operation, the new ChildEntity will need to be persisted.
 //According to my logs, an invocation of EntityManager.persist(anotherNewChild) occurs, during as the merge is propagated to the new entity.
//At this point is where the ChildEntity.otherEntities is detected to be null
return EntityManager.merge(mainEntity); 

问题在于,使用坚持,

List&lt;AnotherEntity&gt;

不为空且不为空,在合并时,

List&lt;AnotherEntity&gt;

为空

我正在通过 ejb 远程调用执行此操作。

休眠 4.3.6 野蝇 8.1.0 jpa 2.1

我这里有什么遗漏吗?

使用以下代码重现问题:

https://github.com/marembo2008/hibernate-jpa-bug

在 Hibernate 问题跟踪器上打开了一个问题。

https://hibernate.atlassian.net/browse/HHH-9751

【问题讨论】:

    标签: java hibernate jpa orm hibernate-mapping


    【解决方案1】:

    问题是合并返回一个新实体,所以你应该这样做:

    mainEntity = EntityManager.merge(mainEntity);
    

    【讨论】:

    • 我就是这么做的。由于我进行远程 ejb 调用,除此之外我不会返回任何内容。请查看我对问题的编辑
    猜你喜欢
    • 2013-03-22
    • 2013-08-12
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2017-04-04
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多