我使用嵌入式OpenEJB容器进行单元测试时遇到了特定问题。我在两个班级之间有双向关系。在一个方向上,关系正常工作,但在相反的方向上,该关系仅在EAGER模式下起作用。在LAZY模式中,字段section
保持为空。剪辑的代码如下:
@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;
}
}
我的代码有点具体。该类仅在内部使用字段sections
来合并所有子集合。我无法填充懒惰部分。我想,容器希望客户端通过getter从外部访问该字段。
答案 0 :(得分:2)
它的内容生命周期问题。必须将所有enties(track及其section)重新附加到持久化上下文。收集事件的方法必须使用EntityManager
在类中。 (该实体不能使用管理器重新附加自身。)更新的实体管理类的示例如下:
public class EntityDataAccessor {
@PersistenceUnit(unitName = "someUnit")
private EntityManagerFactory emFactory;
//gets one track
public Track getTrack(int number) {
EntityManager em = emFactory.createEntityManager();
try {
return (Track)em.find(TrackEntity.class, number);
}
finally {
em.close();
}
}
//the new method collecting events
public Collection<HistoricalEvent> getEventsForTrack(TrackEntity te) {
EntityManager em = emFactory.createEntityManager();
te = em.merge(te); //re-attach to the context
Set<SectionEntity> sections = te.getSections();
TreeSet<HistoricalEvent> set = new TreeSet<HistoricalEvent>();
for (SectionEntity se : sections) {
se = em.merge(se); //re-attach to the context
set.addAll(se.getEvents());
}
em.close();
return set;
}
}
有关详细信息,请参阅问题What's the lazy strategy and how does it work?。