How to de-serialize an object, put it in a managed context, but not save any changes?

I am new to perseverance and I want to do the following:

1) Download the database part in essence and save them in a file

2) Download these objects from the file, put them in a controlled context (so that all @JoinColumn and @JoinTable, especially those that have a lazy fetch type, will work fine) and make sure that I don't accidentally update their database.

The point is that every time I run the application, I do not want to call the database for a table that almost never changes - so I want to store them locally, preferably in a file.

Although part 1) was pretty simple, I have some problems regarding part 2). I realized that loadObject and em.persist () will work fine if I don't allow em.flush ().

But at some point I would like to check if the object in the database has changed. If I call persist () on my de-serialized object and at the same time change the database, after you leave this version?

Is there a better way to do this?

+2
source share
1 answer

Download a β€œmanaged” copy of this object from the database. Then copy the properties from the object you are reading from the file.

You will have a β€œmanaged” object that has not yet been serialized into the database.

Depending on the logic flow, you can interrupt it before combining it with a database instance.

0
source

All Articles