openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daryl Stultz <da...@6degrees.com>
Subject State Management, changed properties on detached object not saving
Date Wed, 01 Jul 2009 12:59:25 GMT
Hello,

This is a continuation of my thread about null value troubles. I've started
a new thread because I've learned more about the nature of the problem
(nothing to do with nulls). Basically I'm changing properties on an entity
while the entity is detached then merging it. The properties changed while
detached are not getting updated. This is wholly unexpected. Here's a unit
test describing my exploration of the issue:

EntityManager em = getEntityManager();
MyEntity entity = em.find(MyEntity.class, entityId);
entity.setProperty(20);
// setting above does not automatically update DB
assertEquals(6, getPropertyFromDb(entityId)); // 6 is the prev value,
getPropertyFromDb(entityId) gets the value straight from DB
// after save, it's the same
entity.save(); // calls em.persist() or em.merge() depending on ID
assertEquals(20, getPropertyFromDb(entityId));
// 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
entity.setProperty(30);
assertEquals(20, getPropertyFromDb(entityId));
OpenJPAEntityManager oem = (OpenJPAEntityManager) em;
assertTrue(oem.isDirty(entity));
// simulate new web transaction
em = getEntityManagerForNewWebTransaction(); // close old em, get new one
oem = (OpenJPAEntityManager) em;
*assertFalse(oem.isDirty(entity)); // didn't expect this!*
entity.save(); // does not write new value
*assertEquals(20, getPropertyFromDb(entityId));*
entity.setProperty(10); // try to dirty it
entity.setProperty(30);
assertTrue(oem.isDirty(entity));
entity.save(); // dirty, but still doesn't write it!
*assertEquals(20, getPropertyFromDb(entityId));*

The bold lines are the unexpected outcomes. It seems an entity may be dirty
only with an em it is currently attached to. I'm not terribly concerned
about the ability to read its dirty state. I'm very concerned by the lack of
update of the column that I changed.

As I stated in my other thread I am using this:
<property name="openjpa.DetachState" value="loaded(AccessUnloaded=false)"/>
which I believe means I'm using DetachedStateField=transient which is
appropriate for staying in the one JVM (I'm not serializing).

What am I doing wrong?
Thanksd.

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

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