Re: Identifying changed fields

From: Bryan Lewis (brya..aine.rr.com)
Date: Tue Oct 26 2004 - 15:56:35 EDT

  • Next message: Derek Rendall: "Prefetching and paginated queries"

    This sounds like a bug I ran into recently, CAY-213. When I updated an
    object by adding to a to-many relationship (and making no other change
    to the object), Cayenne didn't retain a snapshot. When I was trying to
    track it down I got exactly the symptom you describe --
    getDataContext().getObjectStore().getRetainedSnapshot() returned null.

    This was fixed in 1.1RC2 that was released a couple of days ago. If
    you're not yet on that release, I suspect it would fix the problem.

    Steve Steinitz wrote:
    > Hello,
    >
    > Per a helpful suggestion from Andrus earlier in this thread I have
    > used the following technique (in MyObject.validateForUpdate) to
    > identify changed fields in myObject:
    >
    > DataRow freshSnapshot = getDataContext().currentSnapshot(this);
    > DataRow oldSnapshot = getDataContext().getObjectStore().getRetainedSnapshot(this.getObjectId());
    > DataRow diff = (oldSnapshot != null) ?
    > oldSnapshot.createDiff(freshSnapshot) : freshSnapshot;
    >
    > if (diff.size() > 0)
    > {
    > Set changedKeys = diff.keySet();
    > Iterator i;
    > for (i= changedKeys.iterator(); i.hasNext(); )
    > {
    > String key = (String)i.next();
    > Object value = diff.get(key);
    >
    > AuditTrailEntry ate = (AuditTrailEntry)CustomDataObject.
    > createPersistantObject ( getDataContext(),
    > "AuditTrailEntry");
    > ate.setChangedAttribute(key);
    > ate.setChangeDate(new Date());
    > ate.setPreviousValue(oldSnapshot == null ? "-" :
    > (oldSnapshot.get(key) != null ?
    > oldSnapshot.get(key).toString() : "-"));
    > ate.setNewValue(value == null ? "-" : value.toString());
    > addToAuditTrailEntries(lec);
    > ...
    >
    > This works well. However, I was surprised to discover that if I add
    > an object to one of MyObject's relationships to using the following code
    >
    > SubObject aSubObject = (SubObject)createObject("SubObject");
    > myObject.addToSubObjects(aSubObject);
    >
    > then the old snapshot, above:
    > DataRow oldSnapshot = getDataContext().getObjectStore().getRetainedSnapshot(this.getObjectId ())
    > is null so that I can't get a meaningful diff.
    >
    > Can anyone think of a reason this might occur? If I am unable to
    > solve this I may take up Mike's suggestion which he joked was "the
    > hard way" but which appears to have some advantages in specific field
    > handling.
    >
    > Thanks,
    >
    > Steve
    >



    This archive was generated by hypermail 2.0.0 : Tue Oct 26 2004 - 15:56:52 EDT