You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by ljnelson <lj...@gmail.com> on 2009/07/01 16:39:24 UTC

Re: State Management, changed properties on detached object not saving

On Wed, Jul 1, 2009 at 10:03 AM, Daryl Stultz (via Nabble) <
ml-user+140370-1665331873@n2.nabble.com<ml...@n2.nabble.com>
> 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.

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 10:39 AM, ljnelson <lj...@gmail.com> wrote:

>
> 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.
>
>
The merge is effectively the end of the "operational transaction" so I don't
really care that the original is no good. What I'm doing is this:

get managed entity X
detach X by closing em
edit properties of X
get new em
begin em transaction
merge X
commit em transaction
walk away

What's done at "edit properties" is not sent to the database on commit.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Tue, Jul 7, 2009 at 4:45 PM, Michael Dick <mi...@gmail.com>wrote:

>
> I'd use detach. DetachCopy IIRC detaches a new copy of the entity, but
> leaves the original entity in the PersistenceContext.


I started my previous email referencing the JavaDocs but found the code to
be different. I went with detach. There doesn't seem to be a detachCopy in
1.2.1. Not sure what version of JavaDocs I was looking at.


> Evict I'm not sure
> about,


I got that idea from a posting about Hibernate.


> I opened JIRA OPENJPA-1162 [1] on your behalf to add better javadoc.
>

Thanks!

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Michael Dick <mi...@gmail.com>.
On Wed, Jul 1, 2009 at 3:06 PM, Daryl Stultz <da...@6degrees.com> wrote:

> On Wed, Jul 1, 2009 at 3:58 PM, Daryl Stultz <da...@6degrees.com> wrote:
>
> > Is there a way to detach a single entity instance without closing the em?
> >
>
> I see I can cast my em to OpenJPAEntityManager. Should I use detachCopy,
> evict, or detach (which is in the code but not the JavaDocs)? Documentation
> is a little thin.
>

I'd use detach. DetachCopy IIRC detaches a new copy of the entity, but
leaves the original entity in the PersistenceContext. Evict I'm not sure
about, I opened JIRA OPENJPA-1162 [1] on your behalf to add better javadoc.

https://issues.apache.org/jira/browse/OPENJPA-1162


>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: State Management, changed properties on detached object not saving

Posted by Simon Droscher <si...@elasticpath.com>.
You might want to also take a look at the 
http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#openjpa.AutoDetach
openjpa.AutoDetach  property.

You can use this to ensure entities are detached on commit, for example.



Daryl Stultz wrote:
> 
> On Wed, Jul 1, 2009 at 3:58 PM, Daryl Stultz <da...@6degrees.com> wrote:
> 
>> Is there a way to detach a single entity instance without closing the em?
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/State-Management%2C-changed-properties-on-detached-object-not-saving-tp3188832p3198413.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 3:58 PM, Daryl Stultz <da...@6degrees.com> wrote:

> Is there a way to detach a single entity instance without closing the em?
>

I see I can cast my em to OpenJPAEntityManager. Should I use detachCopy,
evict, or detach (which is in the code but not the JavaDocs)? Documentation
is a little thin.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 3:18 PM, Daryl Stultz <da...@6degrees.com> wrote:

>
>
> I don't know yet if the above test represents the problem my application is
> having


Yes! That's it! In a "web transaction" I retrieved an entity, set my
properties and ended the web transaction which closed the em. Then in the
next web transaction I hit the save button which gets an new em and does the
merge. Which doesn't work. I threw in a em.close() before setting my
properties and it works now. I just don't like doing that - my em might need
to lazy load some stuff later. Is there a way to detach a single entity
instance without closing the em?

Thanks Mike! You were a great help!

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 2:11 PM, Michael Dick <mi...@gmail.com>wrote:

> Hi Daryl,
>
> Here's what I've run and I think it does what you're trying to do.


Thanks for working on this Mike. Your example is good - it's interesting
that it can be sent to the database just by begin/commit as you've shown.
You didn't do quite what I want do. Here's what I want:

MyEntity entity = em.find(MyEntity.class, entityId);
entity.setProperty(20);
assertEquals(6, getValueFromDb(entityId)); // default is 6
assertTrue(em.contains(entity));
// after save, it's the same
entity.save();
assertEquals(20, getValueFromDb(entityId));
assertTrue(em.contains(entity));
// simulate new web transaction (Force detach)
em = getEntityManagerForNewWebTransaction();
assertFalse(em.contains(entity));
entity.setProperty(30);
entity.save();
assertFalse(em.contains(entity)); // entity is not the merged instance
assertEquals(30, getValueFromDb(entityId));

This actually works, to my surprise. Trying to make it look like your
example, I eliminated some problem code. Here's a version that fails on the
last line:

MyEntity entity = em.find(MyEntity.class, entityId);
entity.setProperty(20); // setting this does not automatically update DB
assertEquals(6, getValueFromDb(entityId)); // default is 6
// after save, it's the same
entity.save();
assertEquals(20, getValueFromDb(entityId));
*entity.setProperty(30);*
// simulate new web transaction
em = getEntityManagerForNewWebTransaction();
entity.save(); // does not write new value
assertEquals(30, getValueFromDb(entityId)); // FAILS!!!

I don't know yet if the above test represents the problem my application is
having, but the difference is that the property was set while the entity was
still attached to the old em. Is that expected behavior? If I move the bold
line after em is replaced, it works.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Michael Dick <mi...@gmail.com>.
Hi Daryl,

Here's what I've run and I think it does what you're trying to do. FWIW I've
been using 1.2.2-SNAPSHOT for this test and it's passing for me:

    @Test
    public void testDetachUpdateMergeCommit() {
        EntityManager em = emf.createEntityManager();

        Person p = em.find(Person.class, 1);
        em.close(); // force detach

        em = emf.createEntityManager();
        assertFalse(em.contains(p));
        p.setIntProperty(new Integer(10));

        Person p2 = em.merge(p);
        assertFalse(em.contains(p));
        assertTrue(em.contains(p2));

        // No updates have been flushed to the database yet.
        assertEquals(2, getPropertyFromDB());

        // commit a transaction to write to db.
        em.getTransaction().begin();
        em.getTransaction().commit();

        // after transaction commits the database will have the new value.
        assertEquals(10, getPropertyFromDB());
    }

-mike


On Wed, Jul 1, 2009 at 11:28 AM, Daryl Stultz <da...@6degrees.com> wrote:

> On Wed, Jul 1, 2009 at 12:17 PM, Michael Dick <michael.d.dick@gmail.com
> >wrote:
>
> > What does myEntity.getEntityManager() return and how is that set into
> your
> > entity (ie is your code managing it, or are getting it from OpenJPA) ?
> >
>
> I'm using a thread local variable. I've written a variation of the unit
> test
> that replaces the save() method with direct factory/em handling and
> em.merge
> calls to eliminate the possibility of bad logic in my save() method as well
> as my thread local handling. It didn't make any difference. I can post the
> other version if you like.
>
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 12:17 PM, Michael Dick <mi...@gmail.com>wrote:

> What does myEntity.getEntityManager() return and how is that set into your
> entity (ie is your code managing it, or are getting it from OpenJPA) ?
>

I'm using a thread local variable. I've written a variation of the unit test
that replaces the save() method with direct factory/em handling and em.merge
calls to eliminate the possibility of bad logic in my save() method as well
as my thread local handling. It didn't make any difference. I can post the
other version if you like.


-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Michael Dick <mi...@gmail.com>.
What does myEntity.getEntityManager() return and how is that set into your
entity (ie is your code managing it, or are getting it from OpenJPA) ?

On Wed, Jul 1, 2009 at 10:51 AM, Daryl Stultz <da...@6degrees.com> wrote:

> On Wed, Jul 1, 2009 at 11:43 AM, Michael Dick <michael.d.dick@gmail.com
> >wrote:
>
> > 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.
>
>
> (Please see other post for p-code of general pattern.) I *am* committing
> the
> transaction but the values (that should have been copied to the merged
> instance I ignored) are not written to the db.
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: State Management, changed properties on detached object not saving

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 1, 2009 at 11:43 AM, Michael Dick <mi...@gmail.com>wrote:

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


(Please see other post for p-code of general pattern.) I *am* committing the
transaction but the values (that should have been copied to the merged
instance I ignored) are not written to the db.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: State Management, changed properties on detached object not saving

Posted by Michael Dick <mi...@gmail.com>.
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 <lj...@gmail.com> wrote:

>
> On Wed, Jul 1, 2009 at 10:03 AM, Daryl Stultz (via Nabble) <
> ml-user+140370-1665331873@n2.nabble.com<ml...@n2.nabble.com>
> <ml...@n2.nabble.com>
> >
> > 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.
>