Consider the following entities:
@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;
@PrePersist
void prePersist(){
LOG.info(otherEntities); }
}
@Entity
public class AnotherEntity {
}
@Named
@SessionScoped
public class ApplicationController implements Serializable {
private static final long serialVersionUID = -1343434343L;
@EJB
private ParentEntityFacade parentEntityFacade;
private ParentEntity parentEntity;
public void createAndReloadEntity() {
parentEntity = new ParentEntity();
final ChildEntity childEntity = new ChildEntity();
for (int i = 0; i < 10; i++) {
childEntity.getOtherEntities().add(new AnotherEntity());
}
System.out.println(parentEntity);
parentEntity.setChildEntity(childEntity);
parentEntityFacade.create(parentEntity);
this.parentEntity = parentEntityFacade.find(parentEntity.getId());
System.out.println(parentEntity);
}
public void editAndReloadEntity() {
final ChildEntity childEntity = new ChildEntity();
for (int i = 0; i < 10; i++) {
childEntity.getOtherEntities().add(new AnotherEntity());
}
System.out.println(parentEntity);
parentEntity.setChildEntity(childEntity);
this.parentEntity = parentEntityFacade.edit(parentEntity);
System.out.println(parentEntity);
}
}
When
EntityManager.persust(parentEntity)
is invoked, LINE 30 prints the correct list entities
Bu when:
EntityManager.merge(parentEntity)
is invoked, LINE 30 prints null.
We can reproduce the following, using the simple application here in wildfly 8.1.0
https://github.com/marembo2008/hibernate-jpa-bug
|