JPA EclipseLink @OneToMany 返回空集
我正在使用嵌入式 id:
@Embeddable
public class EntityId {
private long id;
}
我有下表:
@Entity
public class Master {
@EmbeddedId private EntityId id;
@OneToMany(mappedBy = "master")
Set<Config> configs;
}
@Entity
public class SlaveLeft {
private @EmbeddedId EntityId id;
}
@Entity
public class SlaveRight {
private @EmbeddedId EntityId id;
}
@Entity
public class Config {
@EmbeddedId private EntityId id;
@ManyToOne
@JoinColumn(name = "MASTER_ID", referencedColumnName = "id")
private Master master;
@ManyToOne
@JoinColumn(name = "LEFT_ID", referencedColumnName = "id")
private SlaveLeft slaveLeft;
@ManyToOne
@JoinColumn(name = "RIGHT_ID", referencedColumnName = "id")
private SlaveRight slaveRight;
}
然后我填充数据库:
final EntityManager em = injector.getInstance(EntityManager.class);
em.getTransaction().begin();
Master master = new Master(EntityId.next());
SlaveLeft slaveLeft = new SlaveLeft(EntityId.next());
SlaveRight slaveRight = new SlaveRight(EntityId.next());
Config config = new Config(EntityId.next(), master, slaveLeft, slaveRight);
em.persist(master);
em.persist(slaveLeft);
em.persist(slaveRight);
em.persist(config);
em.getTransaction().commit();
final Master master1 = em.find(Master.class, master.getId());
System.out.println(master1.getConfigs());
问题是 master1.getConfigs() 返回空。
为了建立工作关系我还缺少什么?
I am using an embedded id:
@Embeddable
public class EntityId {
private long id;
}
And I have the following tables:
@Entity
public class Master {
@EmbeddedId private EntityId id;
@OneToMany(mappedBy = "master")
Set<Config> configs;
}
@Entity
public class SlaveLeft {
private @EmbeddedId EntityId id;
}
@Entity
public class SlaveRight {
private @EmbeddedId EntityId id;
}
@Entity
public class Config {
@EmbeddedId private EntityId id;
@ManyToOne
@JoinColumn(name = "MASTER_ID", referencedColumnName = "id")
private Master master;
@ManyToOne
@JoinColumn(name = "LEFT_ID", referencedColumnName = "id")
private SlaveLeft slaveLeft;
@ManyToOne
@JoinColumn(name = "RIGHT_ID", referencedColumnName = "id")
private SlaveRight slaveRight;
}
I then populate the database:
final EntityManager em = injector.getInstance(EntityManager.class);
em.getTransaction().begin();
Master master = new Master(EntityId.next());
SlaveLeft slaveLeft = new SlaveLeft(EntityId.next());
SlaveRight slaveRight = new SlaveRight(EntityId.next());
Config config = new Config(EntityId.next(), master, slaveLeft, slaveRight);
em.persist(master);
em.persist(slaveLeft);
em.persist(slaveRight);
em.persist(config);
em.getTransaction().commit();
final Master master1 = em.find(Master.class, master.getId());
System.out.println(master1.getConfigs());
The problem is that the master1.getConfigs() returns empty.
What am I missing to get a working relation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用另一个实体管理器来查找您的主人,它的配置集合可能会被填充。但在这里,实体管理器从其缓存中返回主服务器,并且由于您没有将配置放入主服务器的配置集中,因此它不在那里。
使用双向关系时,您有责任维护关系双方。 ORM 仅使用拥有方(没有 MappedBy 属性的一方)来决定关系是否存在,但对象图的一致性是您的责任。
If you use another entity maneger to find your master, its configs collection will probably be populated. But here, the entity manager returns the master from its cache, and since you didn't put the config into the master's set of configs, it isn't there.
When using a bidirectional relationship, you are responsible for the maintenance of both sides of the relationship. The ORM only uses the owning side (the one without the
mappedBy
attribute) to decide if the relationship exists or not, but the coherence of the object graph is your responsibility.