Hibernate Collection在更新时是空的

时间:2015-04-27 20:30:14

标签: java hibernate jpa orm hibernate-mapping

请考虑以下事项:

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

不是null而不是空,而在合并时,

  

List<AnotherEntity>

为空

我在ejb远程调用上这样做。

  

Hibernate 4.3.6   wildfly 8.1.0   jpa 2.1

我在这里缺少什么吗?

使用以下代码重现问题:

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

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

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

1 个答案:

答案 0 :(得分:0)

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

mainEntity = EntityManager.merge(mainEntity);