views:

526

answers:

1

Hi all,

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

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.

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

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

Thanks,

Phil.

+1  A: 

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 single entity.
  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.

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).

ChssPly76