如何反序列化实体,将其置于托管上下文中,但不保留任何更改? [英] How to de-serialize an entity, put it in managed context, but not persist any changes?
问题描述
我是持久性的新手,我想执行以下操作:
I'm new to persistence and I want to do the following:
1)将数据库的一部分加载到实体中并将其存储在文件中
1) Load a part of database into entities and store them in a file
2)从文件中加载这些实体,将它们放在托管上下文中(以便所有@JoinColumn和@JoinTable,尤其是那些具有延迟提取类型的实体都可以正常工作),并确保我不会意外地使用他们.
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.
虽然第1部分非常简单,但我对第2部分有一些担忧.我认为loadObject和em.persist()可以正常工作,只要我确保不调用em.flush()即可.
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().
但是我想检查一下数据库中的对象是否未更改.如果我在反序列化的实体上调用persist(),并且同时更改了数据库,则刷新后仍保留哪个版本?
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?
有更好的方法吗?
推荐答案
从数据库中加载此对象的托管"副本. 然后从您从文件读取的对象中更新属性复制.
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.
这篇关于如何反序列化实体,将其置于托管上下文中,但不保留任何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!