You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Prashant Bhat <pr...@gmail.com> on 2007/09/14 07:53:47 UTC

What is the best approach to refresh/reload the detached entities?

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());
entityManager.refresh(entity);

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?

Thanks,
Prashant

Re: What is the best approach to refresh/reload the detached entities?

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

> 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!
> }

I suspect that em.merge() is closer to what you want. However, merge()
will still return a copy of your instance.

> 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?

When you reattach detached objects, a copy is always returned to you
so that the EntityManager can make sure that it doesn't have duplicate
references in the same persistence context.

Why do you want to avoid making a copy of the object graph? I would
expect that the best way to do what you're trying to do is to merge
your graph and then send it back to the client VM.

-Patrick

On 9/13/07, Prashant Bhat <pr...@gmail.com> wrote:
> 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());
> entityManager.refresh(entity);
>
> 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?
>
> Thanks,
> Prashant
>


-- 
Patrick Linskey
202 669 5907