OpenJPA - 延迟获取不起作用
我在使用嵌入式 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 从外部访问该字段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是实体生命周期的问题。所有实体(轨道及其部分)必须重新附加到持久性上下文。收集事件的方法必须位于使用 EntityManager 的类中。 (实体无法使用管理器重新附加自身。)更新的实体管理类示例如下:
请参阅问题 什么是惰性策略以及它是如何工作的?了解更多详细信息。
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.