JPA: How to synchronize a persistence context with a bulk update or deletion result?

There is an operator in ejb-3_0-fr-spec-persistence.pdf that reads

Save context does not sync to bulk update or delete result

So, if I do query.executeUpdate, which removes the rows from the table. The same lines still exist in other objects from one to many collections. When I restart the application, I see that phantom objects are now removed from the collection.

So, is there a (good \ simple \ general) way to sync a JPA cache with a bulk update result \ delete?

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

Thanks,

Phil.

+5
source share
4 answers

You have to be careful how you use the word cache here because it can mean different things.

The highlighted phrase refers to the persistence context, which can be considered as a “first level cache”. To update it with the latest changes to the database, you can:

  • Call EntityManager.refresh () to update the state of a single object.
  • OR delete an instance of the control object in general (after changing the cleanup / cleanup) and get a new one from the factory object manager. Any objects downloaded from this new instance will be loaded from the database and thus will contain the latest changes.

" ", . (, , ), API ( ).

+5

.

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

1- (EntityManager/transaction) . , clear() EntityManager.

The second level cache (shared cache) should automatically be automatically invalidated when a transaction is completed. If this is not for some reason, you can use the JPA Cache API or the EclipseLink JpaCache API to preempt, invalidate, or update objects.


+1
source

All Articles