Detach an entity from JPA/EJB3 persistence context

后端 未结 14 2108
面向向阳花
面向向阳花 2020-12-13 03:26

What would be the easiest way to detach a specific JPA Entity Bean that was acquired through an EntityManager. Alternatively, could I have a query return detached objects in

相关标签:
14条回答
  • 2020-12-13 04:23

    No matter which JPA implementation you use, Just use entityManager.detach(object) it's now in JPA 2.0 and part of JEE6.

    0 讨论(0)
  • 2020-12-13 04:24

    Unfortunately, there's no way to disconnect one object from the entity manager in the current JPA implementation, AFAIR.

    EntityManager.clear() will disconnect all the JPA objects, so that might not be an appropriate solution in all the cases, if you have other objects you do plan to keep connected.

    So your best bet would be to clone the objects and pass the clones to the code that changes the objects. Since primitive and immutable object fields are taken care of by the default cloning mechanism in a proper way, you won't have to write a lot of plumbing code (apart from deep cloning any aggregated structures you might have).

    0 讨论(0)
提交回复
热议问题