openjpa-users mailing list archives

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

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);
// 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; // 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
assertEquals(20, getPropertyFromDb(entityId));
OpenJPAEntityManager oem = (OpenJPAEntityManager) em;
// simulate new web transaction
em = getEntityManagerForNewWebTransaction(); // close old em, get new one
oem = (OpenJPAEntityManager) em;
*assertFalse(oem.isDirty(entity)); // didn't expect this!*; // does not write new value
*assertEquals(20, getPropertyFromDb(entityId));*
entity.setProperty(10); // try to dirty it
assertTrue(oem.isDirty(entity));; // 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?

Daryl Stultz
6 Degrees Software and Consulting, Inc.

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