openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Prashant Bhat" <>
Subject What is the best approach to refresh/reload the detached entities?
Date Fri, 14 Sep 2007 05:53:47 GMT
Hi All,

I'm using OpenJPA in a standalone application and most of the time the
entities are used in detached state. In some situations, I require the same
detached entity object to be refreshed with the latest data from database.
I've a refresh method in the generic dao, like this:

public E refresh(E entity) {
        if (entity != null && !entity.isNew()) {
            return entityManager.find(entityClass, entity.getId());
        return entity; // entity being refreshed can not be null or new, so
return the same!

But the problem here is, I'll get a new object with the latest data and not
the same object which I passed.
One more option was to use :

entity = entityManager.getReference(entity.getClass(), entity.getId());

Even here, I get a different object.

If I just use, entityManager.refresh(entity); I get the following exception:

<openjpa-1.1.0-SNAPSHOT-r420667:574398 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: Object "
com.sample.entity.Product-com.sample.entity.Product-120" is not managed by
this context.

At present, either I'm reassigning it to the new object or I'm copying all
the properties of the reloaded object back to my original object. Is this
the right approach?


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message