I have a specific problem with an unit test using embedded OpenEJB container. I have a bi-directional relation between two classes. In one direction the relation works properly, but in the opposite direction the relation works only in EAGER-mode. In LAZY-mode the field section stays null. The code snipped follows:
@Entity
@Table(name="tracks")
class TrackEntity implements Track {
@Id
private int trackNumber;
@OneToMany(mappedBy = "track")
private HashSet<SectionEntity> sections;
public TrackEntity() {
sections = new HashSet<SectionEntity>();
}
@Override
public Collection<HistoricalEvent> getEvents() {
if (sections == null)
throw new CommonError("number=" + trackNumber, AppErrors.TRACK_EMPTY);
TreeSet<HistoricalEvent> set = new TreeSet<HistoricalEvent>();
for (SectionEntity se : sections)
set.addAll(se.getEvents());
return set;
}
}
My code is little bit specific. The class uses the field sections just internally to merge all sub-collections. I’m unable to fill sections lazily. I thing, the container expects client to access the field externally via a getter.
Its the problem with life cycle of enties. All enties (track and its sections) must be re-attached to the persistence context. The method collecting events must be in the class using
EntityManager. (The entity cannot use the manager to re-attach itself.) Example of updated entity managing class follows:See question What’s the lazy strategy and how does it work? for more detail.