JPA EclipseLink @OneToMany 返回空集

发布于 2024-11-28 10:26:00 字数 1542 浏览 0 评论 0原文

我正在使用嵌入式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-12-05 10:26:01

如果您使用另一个实体管理器来查找您的主人,它的配置集合可能会被填充。但在这里,实体管理器从其缓存中返回主服务器,并且由于您没有将配置放入主服务器的配置集中,因此它不在那里。

使用双向关系时,有责任维护关系双方。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文