Checking what attributes have changed in a DataObject

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Checking what attributes have changed in a DataObject

Hugi Thordarson
Hi all,
is there some easy way for me in Cayenne to get information (for example a Map) about changes that have been made to a DataObject since it was fetched?

Cheers,
- hugi
Reply | Threaded
Open this post in threaded view
|

Re: Checking what attributes have changed in a DataObject

Lon Varscsak
I implemented this in my base data object class:

    public boolean hasKeyChangedFromCommittedSnapshot(String key) {

        ObjectStore objectStore =
((DataContext)getObjectContext()).getObjectStore(); // Need the object
store to access the snapshot data.

        DataRow dataRow = objectStore.getSnapshot(getObjectId()); // Get
the snapshot data.

        EntityResolver resolver = getObjectContext().getEntityResolver();

        ObjEntity entity = resolver.getObjEntity(getObjectId
().getEntityName());

        ObjAttribute attribute = entity.getAttribute(key);

        String dbAttributeName  = attribute.getDbAttributeName();

        String objAttributeName = attribute.getName();

        Object dataRowValue = (dataRow != null) ? dataRow.get(
dbAttributeName) : null;

        Object currentValue = readPropertyDirectly(objAttributeName);



        return (!Util.nullSafeEquals(dataRowValue, currentValue));

    }


On Tue, Feb 14, 2017 at 9:04 AM, Hugi Thordarson <[hidden email]> wrote:

> Hi all,
> is there some easy way for me in Cayenne to get information (for example a
> Map) about changes that have been made to a DataObject since it was fetched?
>
> Cheers,
> - hugi
Reply | Threaded
Open this post in threaded view
|

Re: Checking what attributes have changed in a DataObject

Michael Gentry-2
In reply to this post by Hugi Thordarson
Something similar to this?

https://gist.github.com/mrg/4dce22b67175c27f4047#file-cayenneutils-java-L149

It uses stdout since I was mainly using it as a debugging tool.


On Tue, Feb 14, 2017 at 11:04 AM, Hugi Thordarson <[hidden email]> wrote:

> Hi all,
> is there some easy way for me in Cayenne to get information (for example a
> Map) about changes that have been made to a DataObject since it was fetched?
>
> Cheers,
> - hugi
Reply | Threaded
Open this post in threaded view
|

Re: Checking what attributes have changed in a DataObject

Michael Gentry-2
Oh, I guess that version uses a logger.  I have several versions lying
around.  Still, same mechanics apply.

mrg


On Tue, Feb 14, 2017 at 11:18 AM, Michael Gentry <[hidden email]>
wrote:

> Something similar to this?
>
> https://gist.github.com/mrg/4dce22b67175c27f4047#file-
> cayenneutils-java-L149
>
> It uses stdout since I was mainly using it as a debugging tool.
>
>
> On Tue, Feb 14, 2017 at 11:04 AM, Hugi Thordarson <[hidden email]>
> wrote:
>
>> Hi all,
>> is there some easy way for me in Cayenne to get information (for example
>> a Map) about changes that have been made to a DataObject since it was
>> fetched?
>>
>> Cheers,
>> - hugi
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Checking what attributes have changed in a DataObject

Hugi Thordarson
Thanks, Lon and Michael!

- hugi


> On 14. feb. 2017, at 16:20, Michael Gentry <[hidden email]> wrote:
>
> Oh, I guess that version uses a logger.  I have several versions lying
> around.  Still, same mechanics apply.
>
> mrg
>
>
> On Tue, Feb 14, 2017 at 11:18 AM, Michael Gentry <[hidden email]>
> wrote:
>
>> Something similar to this?
>>
>> https://gist.github.com/mrg/4dce22b67175c27f4047#file-
>> cayenneutils-java-L149
>>
>> It uses stdout since I was mainly using it as a debugging tool.
>>
>>
>> On Tue, Feb 14, 2017 at 11:04 AM, Hugi Thordarson <[hidden email]>
>> wrote:
>>
>>> Hi all,
>>> is there some easy way for me in Cayenne to get information (for example
>>> a Map) about changes that have been made to a DataObject since it was
>>> fetched?
>>>
>>> Cheers,
>>> - hugi
>>
>>
>>

Reply | Threaded
Open this post in threaded view
|

Re: Checking what attributes have changed in a DataObject

Hugi Thordarson
In reply to this post by Michael Gentry-2
Hi again,

I’m now using this approach to get the changed attribute values, which works fine. But once I start looking at relationship changes, things get complicated.
I do have the foreign key values in the DataRow snapshot, but manually resolving each relationship to objects is somewhat cumbersome. Is there some way I can resolve a relationship's value from the cached snapshot?

Cheers,
- hugi


> On 14. feb. 2017, at 16:18, Michael Gentry <[hidden email]> wrote:
>
> Something similar to this?
>
> https://gist.github.com/mrg/4dce22b67175c27f4047#file-cayenneutils-java-L149
>
> It uses stdout since I was mainly using it as a debugging tool.
>
>
> On Tue, Feb 14, 2017 at 11:04 AM, Hugi Thordarson <[hidden email]> wrote:
>
>> Hi all,
>> is there some easy way for me in Cayenne to get information (for example a
>> Map) about changes that have been made to a DataObject since it was fetched?
>>
>> Cheers,
>> - hugi