I\'ve got an integration test of a DAO in which I use a shared EntityManager (via Spring, using SharedEntityManagerCreator). The test class is marked as @Transactional, as
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.