Java Hibernate集合在更新时为空
考虑以下几点:Java Hibernate集合在更新时为空,java,hibernate,jpa,orm,hibernate-mapping,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(FALS
@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 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);
问题是,使用persist时
列表
在Hibernate问题跟踪程序上打开了一个问题
问题是merge返回一个新实体,因此您应该执行以下操作:
mainEntity = EntityManager.merge(mainEntity);
我就是这么做的。因为我执行远程ejb调用,所以我不会返回除此之外的任何内容。请看我对这个问题的编辑
mainEntity = EntityManager.merge(mainEntity);