开发者

jpa hibernate when is an object managed

开发者 https://www.devze.com 2023-02-12 17:08 出处:网络
I have a test case, where i get an object by id using the entitymanager.find method. then I try to check if the object is in managed state by using entityManager.contains(). but contains comes up fals

I have a test case, where i get an object by id using the entitymanager.find method. then I try to check if the object is in managed state by using entityManager.contains(). but contains comes up false. S开发者_运维技巧o when is an object in the persistant state such that contains return true.


The find-method can be invoked without a transaction, however, in such case the resulting entity (if found) will be in detached state if using EntityManager with "transaction-scoped persistence context". If the EntityManager has "extended persistence context", the result will be in managed state. If the find-method is called within a transaction, the resulting entity will be managed in both cases.

For more information about "transaction-scoped" vs. "extended" -persistence contexts, see here.


If you call find by id in the entity manager, the object will be managed while still within the transaction. Otherwise it becomes detached and you have to call entityMangerInstance.merge() to make an object managed.

0

精彩评论

暂无评论...
验证码 换一张
取 消