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

Andrus Adamchik <[email protected]>
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
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 <[email protected]> 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
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.