I am facing an issue, where I need to retrieve an entity two times in read-only mode to compare them ( need to have to seperate object's from db pointing the same entity, and before comparing I make some operations on them ). Since my JPA and Hibernate are not supporting detach() in EntityManager, and I am not able to change it to a newer version, is there any workaround for that so I would be able to fetch those two ?
我正面临一个问题,我需要在只读模式下检索一个实体两次来比较它们(需要从db指向同一个实体分离对象,在比较之前我对它们进行一些操作)。由于我的JPA和Hibernate不支持EntityManager中的detach(),并且我无法将其更改为更新的版本,是否有任何解决方法,以便我可以获取这两个?
Thank you in advance for your help,
预先感谢您的帮助,
P.
3 个解决方案
#1
0
You might transfer your entity into another bean like MyUnmanagedEntity
after fetching. Simply use a subclass that is not mapped as @Entity
and therefor won't be persisted. Init it using a proper constructor that takes the original and transfers all the fields you'll need.
您可以在获取后将实体转移到MyUnmanagedEntity之类的另一个bean中。只需使用未映射为@Entity的子类,因此不会保留。使用适当的构造函数初始化它,该构造函数接收原始文件并传输您需要的所有字段。
Another way is to close()
your EntityManager
so it stops managing your entities. But this would affect all other managed entities as well.
另一种方法是关闭()您的EntityManager,以便它停止管理您的实体。但这也会影响所有其他管理实体。
#2
0
I think you can obtain underlying Hibernate Session
with em.getDelegate()
, and call evict()
on it.
我想你可以用em.getDelegate()获取底层的Hibernate Session,并在其上调用evict()。
#3
0
I have managed to achieve my goal by a workaround, using one of the features of Apache Commons library, and more precisely Common BeanUtils ( http://commons.apache.org/beanutils/ ). It contains a way to copy the content of entity bean, which is detached, and further process without affecting the data in database.
我已经设法通过一个变通方法,使用Apache Commons库的一个功能,更准确地说是Common BeanUtils(http://commons.apache.org/beanutils/)来实现我的目标。它包含一种复制实体bean内容的方法,该方法是分离的,并且进一步处理而不影响数据库中的数据。
Thank you for all help,
谢谢你的帮助,
Regards, P.
#1
0
You might transfer your entity into another bean like MyUnmanagedEntity
after fetching. Simply use a subclass that is not mapped as @Entity
and therefor won't be persisted. Init it using a proper constructor that takes the original and transfers all the fields you'll need.
您可以在获取后将实体转移到MyUnmanagedEntity之类的另一个bean中。只需使用未映射为@Entity的子类,因此不会保留。使用适当的构造函数初始化它,该构造函数接收原始文件并传输您需要的所有字段。
Another way is to close()
your EntityManager
so it stops managing your entities. But this would affect all other managed entities as well.
另一种方法是关闭()您的EntityManager,以便它停止管理您的实体。但这也会影响所有其他管理实体。
#2
0
I think you can obtain underlying Hibernate Session
with em.getDelegate()
, and call evict()
on it.
我想你可以用em.getDelegate()获取底层的Hibernate Session,并在其上调用evict()。
#3
0
I have managed to achieve my goal by a workaround, using one of the features of Apache Commons library, and more precisely Common BeanUtils ( http://commons.apache.org/beanutils/ ). It contains a way to copy the content of entity bean, which is detached, and further process without affecting the data in database.
我已经设法通过一个变通方法,使用Apache Commons库的一个功能,更准确地说是Common BeanUtils(http://commons.apache.org/beanutils/)来实现我的目标。它包含一种复制实体bean内容的方法,该方法是分离的,并且进一步处理而不影响数据库中的数据。
Thank you for all help,
谢谢你的帮助,
Regards, P.