tags:

views:

39

answers:

1

I'm using OpenJPA in my application as a JPA vendor.

The question is theoretical or conceptual:

Is there any way to tell an entity manager to load an entity from the DB rather than from it's cache?

The problematic scenario:

EM1.persist(Entity1)
EM2.merge(Entity1)
EM1.find(Entity1)  <--- Entity1 is the cached version rather than the merged one..

Any elegant way to do it? I really don't want to call em.refresh(entity).

A: 

If you have the entity available then em.refresh(entity) is the cleanest way to force the entity to be reloaded.

If you don't have the entity available you can call:

EM1.clear(); // all entities are detached - might not be desired.  
EM1.find(Entity1);

In JPA 2.0 you could also explicitly detach the entity (but I don't see this as being better than em.refresh()):

EM1.detach(Entity); 
EM1.find(Entity1);
Mike