StateManager savedImage retains references - not the original values


ebenzacar@...
 
Edited

Hi,

I've been trying to work with the StateManager and its savedImage object which I had expected to be a representation of the current object at the time that "saveFields()" is called.  Unfortunately, I just noticed that this is not the case for any Array / Collection elements.  That is to say, that the array object is copied from the source Persistable to the savedImage,  but as a shallow-copy only; the savedImage retains the same Collection/Array object as the source.  Which means that any changes to the source Collection will be reflected in the savedImage's collection as well.

To be more clear, here is some sample objects I'm working with:

Persistable:
public class Address implements Detachable, Persistable { @Join(column="ID") @Element(column="ELEMENT") Street[] street;
....
....
protected final void dnCopyField(Address obj, int index) { switch (index) { case 0: this.street = obj.street; break; default: throw new IllegalArgumentException("out of field index :" + index); } }

...

}


StateManagerImpl:

public void
saveFields()
{
savedImage = myPC.dnNewInstance(this);
savedImage.dnCopyFields(myPC, cmd.getAllMemberPositions());
savedPersistenceFlags = persistenceFlags;
savedLoadedFields = loadedFields.clone();
}


A few questions:
1) Is this intentional?
2) Is there anyway to override this behaviour, and keep a deep copy of the value instead of a referential value only?
3) Is there any other way I can capture the original value of a Collection/Array/Map/etc prior to it being updated/modified?  Are there any methods in the StateManager (or elsewhere) that can identify when the value of Collection/etc is being modified?
I read in the DN docs regarding SCOs:
> proxy : whether the field is represented by a "proxy" that intercepts any operations to detect whether it has changed internally.

Where can I find the implementation of this "proxy"?  Is it enabled automatically/by default?  Do I need to enable a flag in the dn persistence manager to enable it?

Thanks,

Eric

Join main@datanucleus.groups.io to automatically receive all group messages.