EntityManager查询后不刷新数据

发布于 2024-10-22 10:47:16 字数 388 浏览 3 评论 0原文

我当前的项目使用 HSQLDB2.0 和 JPA2.0 。

场景是:我查询数据库以获取 personcontactDetails 列表。我在 UI 中删除单个 contactInfo 但不保存该数据(取消 保存部分)。

我再次执行相同的查询,现在结果列表比之前的结果少 1,因为我在 UI 中删除了一个 contactInfo。但如果我交叉检查,该 contactInfo 在数据库中仍然可用。

但是,如果我在查询开始之前包含 entityManager.clear() ,我每次都会得到正确的结果。

我不明白这种行为。有人能为我说清楚吗?

My current project uses HSQLDB2.0 and JPA2.0 .

The scenario is: I query DB to get list of contactDetails of person. I delete single contactInfo at UI but do not save that data (Cancel the saving part).

I again do the same query, now the result list is 1 lesser than previous result coz I have deleted one contactInfo at UI. But that contactInfo is still available at DB if I cross check.

But if I include entityManager.clear() before start of the query, I get correct results every time.

I dont understand this behaviour. Could anyone make it clear for me?

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

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

发布评论

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

评论(2

ˉ厌 2024-10-29 10:47:16

不要再次查询,请尝试以下操作:

entityManager.refresh(person);

更完整的示例:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("...");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();

Person p = (Person) em.find(Person.class, 1);
assertEquals(10, p.getContactDetails().size()); // let's pretend p has 10 contact details
p.getContactDetails().remove(0);
assertEquals(9, p.getContactDetails().size());

Person p2 = (Person) em.find(Person.class, 1);
assertTrue(p == p2); // We're in the same persistence context so p == p2
assertEquals(9, p.getContactDetails().size());

// In order to reload the actual patients from the database, refresh the entity
em.refresh(p);
assertTrue(p == p2);
assertEquals(10, p.getContactDetails().size());
assertEquals(10, p2.getContactDetails().size());

em.getTransaction().commit();
em.close();
factory.close();

Rather than querying again, try this:

entityManager.refresh(person);

A more complete example:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("...");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();

Person p = (Person) em.find(Person.class, 1);
assertEquals(10, p.getContactDetails().size()); // let's pretend p has 10 contact details
p.getContactDetails().remove(0);
assertEquals(9, p.getContactDetails().size());

Person p2 = (Person) em.find(Person.class, 1);
assertTrue(p == p2); // We're in the same persistence context so p == p2
assertEquals(9, p.getContactDetails().size());

// In order to reload the actual patients from the database, refresh the entity
em.refresh(p);
assertTrue(p == p2);
assertEquals(10, p.getContactDetails().size());
assertEquals(10, p2.getContactDetails().size());

em.getTransaction().commit();
em.close();
factory.close();
如何视而不见 2024-10-29 10:47:16

clear() 的行为在其

清除持久性上下文,导致所有托管实体分离。 对尚未刷新到数据库的实体所做的更改将不会保留。

也就是说,contactInfo 的删除不会持久。

ContactInfo 不会从数据库中删除,因为您删除了 ContactDetailsContactInfo 之间的关系,但没有删除 ContactInfo 本身。如果您想删除它,则需要使用 remove() 显式执行此操作,或者在关系上指定 orphanRemoval = true

The behaviour of clear() is explained in its javadoc:

Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

That is, removal of contactInfo is not persisted.

ContactInfo is not getting removed from the database because you remove the relationship between ContactDetails and ContactInfo, but not ContactInfo itself. If you want to remove it, you need either do it explicitly with remove() or specify orphanRemoval = true on the relationship.

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