You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@karlmenn.is> on 2021/01/03 19:32:43 UTC

Storing a column containing a JSON-object before save

Hi all,
I've got a text column containing an object (or rather; an object structure) that's been serialized to a JSON string. I'm currently just re-serializing every time I change something—not very nice— so I'd like to do something…nicer. I.e. preferably, cayenne would know that something within the structure has changed, and take care of re-serializing for me (or let me know I need to re-serialize before commit).
I could re-serialize everything before every commit, but for performance reasons I'd prefer to perform re-serialization only if the structure has actually changed.

First thought was creating a custom ValueObjectType and just model the type directly, but it seems like a bit of an overkill. Or perhaps it's not?

Grateful for any suggestions.

Cheers,
- hugi


Re: Storing a column containing a JSON-object before save

Posted by Hugi Thordarson <hu...@karlmenn.is>.
Hi Andrus, happy new year!

The stuff in 4.2 is really promising, look forward to trying it out.
But yeah, going the ValueObjectType route for now :).

Thanks!
- hugi



> On 4 Jan 2021, at 10:54, Andrus Adamchik <an...@objectstyle.org> wrote:
> 
> Hi Hugi,
> 
> FWIW, we are in the process of solving this exact problem of detecting changes in a complex value object for Cayenne's own JSON type available since 4.2:
> 
> https://github.com/apache/cayenne/pull/443 <https://github.com/apache/cayenne/pull/443>
> 
> In our case we had to do something even worse - parse a serialized JSON String to JSON to do semantic comparison to detect all possible invariants.
> 
> In your case a custom ValueObjectType is the way to go. You can simply implement a proper "equals" method in your object, and provide a ValueObjectType that will do (de)serialization. It will only be called when and if needed (i.e. when a change is detected by the "equals" method). This is actually a good solution that is going to perform faster than the above.
> 
> Andrus
> 
> 
>> On Jan 3, 2021, at 10:32 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>> 
>> Hi all,
>> I've got a text column containing an object (or rather; an object structure) that's been serialized to a JSON string. I'm currently just re-serializing every time I change something—not very nice— so I'd like to do something…nicer. I.e. preferably, cayenne would know that something within the structure has changed, and take care of re-serializing for me (or let me know I need to re-serialize before commit).
>> I could re-serialize everything before every commit, but for performance reasons I'd prefer to perform re-serialization only if the structure has actually changed.
>> 
>> First thought was creating a custom ValueObjectType and just model the type directly, but it seems like a bit of an overkill. Or perhaps it's not?
>> 
>> Grateful for any suggestions.
>> 
>> Cheers,
>> - hugi

Re: Storing a column containing a JSON-object before save

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Hugi,

FWIW, we are in the process of solving this exact problem of detecting changes in a complex value object for Cayenne's own JSON type available since 4.2:

https://github.com/apache/cayenne/pull/443 <https://github.com/apache/cayenne/pull/443>

In our case we had to do something even worse - parse a serialized JSON String to JSON to do semantic comparison to detect all possible invariants.

In your case a custom ValueObjectType is the way to go. You can simply implement a proper "equals" method in your object, and provide a ValueObjectType that will do (de)serialization. It will only be called when and if needed (i.e. when a change is detected by the "equals" method). This is actually a good solution that is going to perform faster than the above.

Andrus


> On Jan 3, 2021, at 10:32 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
> 
> Hi all,
> I've got a text column containing an object (or rather; an object structure) that's been serialized to a JSON string. I'm currently just re-serializing every time I change something—not very nice— so I'd like to do something…nicer. I.e. preferably, cayenne would know that something within the structure has changed, and take care of re-serializing for me (or let me know I need to re-serialize before commit).
> I could re-serialize everything before every commit, but for performance reasons I'd prefer to perform re-serialization only if the structure has actually changed.
> 
> First thought was creating a custom ValueObjectType and just model the type directly, but it seems like a bit of an overkill. Or perhaps it's not?
> 
> Grateful for any suggestions.
> 
> Cheers,
> - hugi
>