如何在PreUpdate的JPA中比较两个实体 [英] Howto compare two entities in JPA onPreUpdate

查看:91
本文介绍了如何在PreUpdate的JPA中比较两个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenJPA 2.2.1,并希望执行以下操作:

I am working with OpenJPA 2.2.1 and would like to do the following:

1)加载实体2)变更实体3)在保存实体之前检查所做的更改

1) Load an entity 2) Change the entity 3) Check the changes done before saving the entity

我所做的:

1)通过

    EntityManager.find(MyObject.class, id);

2)好吧,我想很清楚,类似

2) Ok, pretty clear, I think, something like

    MyObject obj = EntityManager.find(MyObject.class, id);
    obj.setName("New name");

3)试图从PersistenceContext中驱逐该对象,然后通过以下方式从数据库中再次加载它:

3) Tried to evict the object from the PersistenceContext and loading it again from the database via:

    EntityManager.evict(MyObject.class, id);
    EntityManager.find(MyObject.class, id);

在3)中的find()调用返回的实体始终等于(在ID中)在1)中找到的实体

The entity returned by the find() call in 3) is always equal (equal ID) to the entity found in 1)

我以为可以通过逐出从PersistenceContext/缓存中删除实体.

I thought I could remove the entity from the PersistenceContext / cache by evict.

如何实现拥有两个不同的实体:a)变更后的实体和b)步骤3)中加载的数据库中的原始实体

How can I achieve to have two different entities: a) the changed entity and b) the original entity from the database loaded in step 3)

并且:如果我刷新实体,则我期望find方法返回刷新的实体.正确吗?

And: If i refresh() the entity I expect the find method to return the refreshed one. Is that correct?

我也尝试了@PreUpdate侦听器,但结果相同,所以我认为对于JPA PersistenceContext或Java引用,肯定有一些我不了解的地方...

I tried the @PreUpdate listeners too with the same result so I think there must be something I have not understood regarding JPA PersistenceContext or Java references...

希望我能提供足够的信息!预先感谢!

I hope I provided enough information! Thanks in advance!

推荐答案

对于JPA生命周期的良好概述,我发现此站点(尤其是状态图)非常有用:

For a good overview of the JPA lifecycle I found this site (particularly the state diagram) to be helpful: http://www.objectdb.com/java/jpa/persistence/managed

主要要点是,从持久性上下文中删除对象时,它们不会消失".您的 obj 仍将指向完全初始化的 MyObject ,并且 name 设置为新名称".唯一的区别是,提交事务/刷新持久性上下文将不再更新相应的数据库条目.

The main take-away is that objects don't "disappear" when you remove them from the persistence context. Your obj will still point to a fully initialized MyObject with name set to "New name". The only difference is that committing the transaction/flushing the persistence context will no longer update the corresponding database entry.

要提交给数据库之前要查看对 obj 的更改,您有几种选择.如果足够的话,我建议

You have several options when you want to look at the changes to obj before committing to the database. If quick and dirty suffices, I'd suggest

MyObject original = obj.clone();

obj 原始与您喜欢的任何工具进行比较(或仅将它们均打印到日志中).

and compare obj and original with whatever tool (or just plain printing both to your log) you like.

关于 refresh(),看看此页面说:

内存中托管对象的内容将被丢弃(包括更改,如果有的话),并将其替换为从数据库中检索到的数据.这对于确保应用程序处理实体对象的最新版本可能很有用,以防万一自从检索到它以来可能已被另一个EntityManager更改过.

The content of the managed object in memory is discarded (including changes, if any) and replaced by data that is retrieved from the database. This might be useful to ensure that the application deals with the most up to date version of an entity object, just in case it might have been changed by another EntityManager since it was retrieved.

这篇关于如何在PreUpdate的JPA中比较两个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆