Hi Daryl,

Some comments based on your post (selected snippets below) :

// push into managed context and replace instance
> entity = em.merge(entity);
> // I thought an update on object would be live to DB, but it's not
>
> The update is not live to the DB. What you've done here is merge a detached
instance into the PersistenceContext - not into the DB. The
PesistenceContext can be thought of as the state of a set of entities in
memory (sort of a L1 cache). When the PersistenceContext is flushed your
changes will go to the database. When the transaction is committed they'll
be committed in the DataBase.

The remaining issues look like they happen because the entity is trying to
manage itself. As Laird indicated em.merge(myEntity) doesn't add myEntity to
the PersistenceContext, it creates a new copy of myentity. The new copy is
part of the PersistenceContext, but myEntity is not.

-mike


On Wed, Jul 1, 2009 at 9:39 AM, ljnelson <[email protected]> wrote:

>
> On Wed, Jul 1, 2009 at 10:03 AM, Daryl Stultz (via Nabble) <
> [email protected]<ml-user%[email protected]>
> <ml-user%[email protected]<ml-user%[email protected]>
> >
> > wrote:
>
> > To be a little more clear, entity.save() looks like this:
> >
> > EntityManager em = getEntityManager();
> > boolean alreadyInTransaction = em.getTransaction().isActive();
>
> if (! alreadyInTransaction) em.getTransaction().begin();
>
> if (getId() == null) em.persist(this);
>
> else em.merge(this);
> >
> if (! alreadyInTransaction) em.getTransaction().commit();
>
>
> I have to admit to only cursorily (is that a word?) scanning this, but
> remember that merge() has a return value, and the return value supersedes
> the value you passed into it.
>
> You need to follow this pattern when merging:
>
> final X newThing = em.merge(oldThing);
>
> ...and then "throw out" oldThing, since it is effectively no longer valid.
>
> Given that you are merge()ing "this", that might be difficult.  :-D
>
> I hope that helps at least somewhat.  Good luck.
>
> Best,
> Laird
>
> --
> View this message in context:
> http://n2.nabble.com/State-Management%2C-changed-properties-on-detached-object-not-saving-tp3188832p3189415.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Reply via email to