休眠集合在更新时为空



请考虑以下事项:

@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<AnotherEntity>

不为空也不为空,而在合并时,

List<AnotherEntity>

为空

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

休眠 4.3.6 野蝇 8.1.0 日本邮政 2.1

我在这里缺少什么吗?

使用以下代码重现问题:

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

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

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

问题是 merge 会返回一个新实体,所以你应该做这样的事情:

mainEntity = EntityManager.merge(mainEntity);