EntityManager.persist 负载相关实体问题

发布于 2024-12-02 02:46:14 字数 530 浏览 4 评论 0原文

我对 EntityManager.persist 有问题(我使用 glassfish 的 tooplink)。

我的代码是:

MyEntity e = new MyEntity();
MyEntityPK ePK = new MyEntityPK();
ePK.setField1("aaa");
ePK.setField2("bbb");

e.setMyEntityPK(ePK);
e.setFiled3(ccc);

entityManager.persist(e);

//HERE NOT PROBLEM
//If a i want a related entity, i get null

AnotherEntity anotherEntity = e.getAnotherEntity();
//a is null

为什么“anotherEntity”为空?持久化后,对象开始附加到 PersistenceContext。 为什么如果我调用 getAnotherEntity() 则相关实体未加载

坦克!

路易吉

i have a problem with EntityManager.persist (i use tooplink of glassfish).

My code is:

MyEntity e = new MyEntity();
MyEntityPK ePK = new MyEntityPK();
ePK.setField1("aaa");
ePK.setField2("bbb");

e.setMyEntityPK(ePK);
e.setFiled3(ccc);

entityManager.persist(e);

//HERE NOT PROBLEM
//If a i want a related entity, i get null

AnotherEntity anotherEntity = e.getAnotherEntity();
//a is null

Why "anotherEntity" is null? After a persist, the object begin attached to PersistenceContext.
Why the related entity are not loaded if i call getAnotherEntity()

Tanks!

Luigi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-12-09 02:46:14

persist() 保留(插入)对象。它会持久化您传递给它的内容,对象在持久化之前和之后将保持不变,除了可能分配生成的 id 之外。

您似乎想要找到一个现有的实体,而不是保留一个新的实体。为此,您应该使用 find() 而不是 persist。

persist() persists (inserts) the object. It persists what you pass it, the object will remain the same before and after the persist, except for a possible assign generated id.

You seem to want to find an existing entity not persist a new one. For this you should use find() not persist.

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