JPA / How to fetch entity in read-only mode?
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 ?
Thank you in advance for your help,
P.
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.
Another way is to close()
your EntityManager
so it stops managing your entities. But this would affect all other managed entities as well.
I think you can obtain underlying Hibernate Session
with em.getDelegate()
, and call evict()
on it.
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.
Thank you for all help,
Regards, P.
精彩评论