Conflicting results from calls to NucleusJDOHelper.isDirty()


ebenzacar@...
 
Edited

I'm trying to identify which fields of a Persistable object are dirty, but running into what seems to be conflicting information from the NucleusJDOHelper functions.  I don't know if this is expected behaviour, or a bug.

When creating a new Persistable object, in an event listener, I can retrieve the persistable object and check:

{ 
tx.begin();

Student p = new Student("First Student");
p.setAddress(new Address(new Street[]{new Street("Regina")}));

pm.makePersistent(p);

tx.commit();
}


public void
postStore(InstanceLifecycleEvent event) {
Persistable pc = (Persistable)event.getSource();
PersistenceManager pm = (PersistenceManager)pc.dnGetExecutionContext().getOwner();
NucleusLogger.GENERAL.info("Audit : postStore for " + ((Persistable) event.getSource()).dnGetObjectId());
NucleusLogger.GENERAL.info("Audit: Object Dirty : " + NucleusJDOHelper.isDirty(pc));
NucleusLogger.GENERAL.info("Audit: Dirty Fields : " + NucleusJDOHelper.getDirtyFields(pc, pm));
}


I get the following output:

16:23:04,262 (main) INFO  [DataNucleus.General] - Audit : postStore for mydomain.model.Student-1
16:23:04,262 (main) INFO [DataNucleus.General] - Audit: Object Dirty : true
16:23:04,262 (main) INFO [DataNucleus.General] - Audit: Dirty Fields : null


Is that expected behaviour?  That the list of dirty fields is empty if it is a new object, but the object is considered dirty?
If so, is there any way to get a list of the fields that have been set in the new object?

Thanks,

Eric


Andy
 

JDOHelper.isDirty returns based on the object state (JDOHelper.getObjectState) and yours is P_NEW.
NucleusJDOHelper(DataNucleusHelperJDO in v6).getDirtyFields returns based on any specific fields being marked dirty due to being updated. Your fields have not been updated (since meeting the persistence process and having a StateManager assigned).

JDOHelper.getObjectState tells you extra info about what type of operation is happening and you should make use of that


ebenzacar@...
 

On Thu, Sep 23, 2021 at 07:55 AM, Andy wrote:
NucleusJDOHelper(DataNucleusHelperJDO in v6).getDirtyFields returns based on any specific fields being marked dirty due to being updated. Your fields have not been updated (since meeting the persistence process and having a StateManager assigned).

Thanks; that is what I suspected, but wasn't sure.  I am using the ObjectProvider to get the LifecycleState and use the boolean helpers (ie: isNew(), isDeleted(), isDirty(), etc); I suspect that is similar (other than the LifecycleState being a DN construct and not a JDO standard)?


I would have expected that for a P_NEW object, all non-null fields would be identified as Dirty (even though they were modified prior to having the StateManager assigned).  From my perspective, a `dirty` field/object is a value which is not representative of the corresponding value in the DataStore.  Given that the object is P_NEW, by definition, none of the fields have been persisted, and consequently I would expect them to all be "dirty".

I am not sure how other ORMs handle field states of NEW objects.  Is this concept of NEW objects not having dirty fields unique to DN or is there a particular design reason/decision that this was approached like this?

Thanks,

Eric