Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityManager refresh

I have web application using JPA. This entity manager keeps bunch of entites and suddenly I update the database from other side. I use MySQL and I use PhpMyAdmin and change some row.

How to tell entity manager to re-synchronize, e.g. to forgot all the entites in cache?

I know there is refresh(Object) method, but is there any possibility how to do refreshAll() or something what results in this?

It is sure this is expensive operation but if it has to be done.

like image 411
zdenda.online Avatar asked Apr 29 '11 13:04

zdenda.online


2 Answers

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

Refresh is something different since it modifies your object. This line will just empty the cache, so if you fetch objects changed outside the entity manager, it will do an actual database query instead of using the outdated cached value.

like image 183
Rasmus Franke Avatar answered Nov 07 '22 19:11

Rasmus Franke


I had a similar issue and the evictAll() line above worked for me.

Alternatively, the @Cache annotation on the entity class worked too, with the benefit of being able to control caching parameters:

@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS)

See: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching

like image 26
Arnold Avatar answered Nov 07 '22 19:11

Arnold