java JPA:如何将持久化上下文与批量更新或删除的结果同步?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1468674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:39:22  来源:igfitidea点击:

JPA: How do I synchronize the persistence context with the result of the bulk update or delete?

javajpaeclipselink

提问by Phil

There's a statement in the ejb-3_0-fr-spec-persistence.pdf which reads

ejb-3_0-fr-spec-persistence.pdf 中有一条声明,内容为

The persistence context is not synchronized with the result of the bulk update or delete

持久化上下文与批量更新或删除的结果不同步

So if I do a query.executeUpdate which deletes rows from a table. Those same rows still exist in another entities one to many collection. When I restart the application, I see the phantom entities are now removed from the collection.

因此,如果我执行 query.executeUpdate 从表中删除行。这些相同的行仍然存在于另一个实体一对多集合中。当我重新启动应用程序时,我看到幻影实体现在已从集合中删除。

So is there a (nice\simple\generic) way of synchronizing JPA's cache with the result of a bulk update\delete ?

那么是否有一种(不错的\简单的\通用的)方法可以将 JPA 的缓存与批量更新\删除的结果同步?

BTW. Im using EclipseLink, version: Eclipse Persistence Services - 1.1.0.r3634.

顺便提一句。我使用 EclipseLink,版本:Eclipse Persistence Services - 1.1.0.r3634。

Thanks,

谢谢,

Phil.

菲尔。

回答by ChssPly76

You have to be careful of how you use the word "cache" here for it may mean different things.

你必须小心你在这里如何使用“缓存”这个词,因为它可能意味着不同的东西。

The highlighted phrase talks about persistence context, which can be thought of as "1st level cache". In order to update it with the latest changes from the database you can either:

突出显示的短语谈论持久性上下文,可以将其视为“一级缓存”。为了使用数据库中的最新更改更新它,您可以:

  1. Call EntityManager.refresh()to refresh state of a singleentity.
  2. OR discard entity manager instance altogether (after flushing / clearing changes as appropriate) and obtain a new one from entity manager factory. Any entities you load from within this new instance will be loaded from database and thus contain latest changes.
  1. 调用EntityManager.refresh()来刷新单个实体的状态。
  2. 或完全丢弃实体管理器实例(在适当的情况下刷新/清除更改后)并从实体管理器工厂获取一个新实例。您从这个新实例中加载的任何实体都将从数据库加载,因此包含最新的更改。

Then there also may be a "2nd level cache" which is not bound to a particular entity manager. You can refresh it (or, rather, clear and let it repopulate itself) using its own API (differs between cache providers).

然后也可能有一个“二级缓存”,它没有绑定到特定的实体管理器。您可以使用它自己的 API(缓存提供程序之间不同)刷新它(或者,更确切地说,清除并让它重新填充自己)。

回答by FAtBalloon

This is how you clear the cached data.

这就是清除缓存数据的方式。

entityManager.getEntityManagerFactory().getCache().evictAll();

回答by James

The 1st level cache (EntityManager/transaction) will need to be manually refreshed or cleared. You can either refresh the objects, call clear(), or get a new EntityManager.

一级缓存(EntityManager/transaction)需要手动刷新或清除。您可以刷新对象、调用 clear() 或获取新的 EntityManager。

The 2nd level cache (shared cache) should get automatically invalidated when you commit the transaction. If it is not for some reason, then you can use the JPA Cache API, or EclipseLink JpaCache API to evict or invalidate the objects, or you could refresh them.

当您提交事务时,二级缓存(共享缓存)应该会自动失效。如果不是出于某种原因,那么您可以使用 JPA Cache API 或 EclipseLink JpaCache API 来驱逐或使对象无效,或者您可以刷新它们。



回答by Puspendu Banerjee

The persistence context is not updated to reflect results of update and delete operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction (see Introduction to EclipseLink Transactions). That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.

持久化上下文不会更新以反映更新和删除操作的结果。如果您使用事务范围的持久性上下文,那么您应该自己执行事务中的批量操作,或者成为事务中的第一个操作(请参阅 EclipseLink 事务简介)。这是因为持久化上下文主动管理的任何实体都不会意识到数据库级别发生的实际更改。

Fell comfortable to knock me here at http://puspendu.wordpress.com/2010/12/22/sync-jpa-database-multiple-application/

http://puspendu.wordpress.com/2010/12/22/sync-jpa-database-multiple-application/敲我这里很舒服