How to de-serialize an entity, put it in managed context, but not persist any changes?

余生颓废 提交于 2019-12-24 00:35:10

问题


I'm new to persistence and I want to do the following:

1) Load a part of database into entities and store them in a file

2) Load these entities from file, put them in managed context (so that all the @JoinColumn and @JoinTable, especially those with lazy fetch type, would work normally) and make sure that I don't accidentally update database with them.

The point of all that is that every time I start the application I don't want to call database for the table that almost never change - so I want to store them locally, preferably in a file.

While part 1) was pretty easy, I have some concerns as to part 2). I figured that loadObject and em.persist() would work fine as long as I make sure not to call em.flush().

But at one point I'd like to check if the object in database hasn't been changed. If I call persist() on my de-serialized entity, and at the same time database changed, after flushing which version would remain?

Is there a better way to do this?


回答1:


Load a "managed" copy of this object from database. Then update copy the properties over from the object you read from file.

You'll have a "managed" entity not yet serialized into database.

Depending on your flow of logic you can abort it before you merge it with database instance.



来源:https://stackoverflow.com/questions/6815708/how-to-de-serialize-an-entity-put-it-in-managed-context-but-not-persist-any-ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!