JPA: Does EntityManager.find() always return the same object reference for the same key?

一曲冷凌霜 提交于 2019-12-01 22:27:31

find() returns the same instance inside a scope of persistence context.

In the case of shared EntityManager (container-managed transaction-scoped persistence context, in JPA Spec terms) lifecycle of persistence context is bound to the transaction, therefore find() returns the same instance when called from the same transaction. I guess in your case setup of your test doesn't happen in the same transaction as a test method, so find() produces different instances.

No it does not. You should rely on object EQUALITY instead of IDENTITY anyway. Override the equals method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!