简体   繁体   English

JPA:EntityManager.find()是否始终为同一个键返回相同的对象引用?

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

I've got an integration test of a DAO in which I use a shared EntityManager (via Spring, using SharedEntityManagerCreator). 我有一个DAO的集成测试,我在其中使用共享的EntityManager(通过Spring,使用SharedEntityManagerCreator)。 The test class is marked as @Transactional, as is the DAO method under test. 测试类标记为@Transactional,测试中的DAO方法也是如此。

In both the test class and the DAO I'm retreiving a User entity as follows: 在测试类和DAO中,我正在检索User实体,如下所示:

User user = em.find(User.class, "test");

In the setup of my test I've modified the user object, but I wasn't seeing the modification in the DAO when the test came to run. 在我的测试设置中,我修改了用户对象,但是当测试开始运行时,我没有看到DAO中的修改。 It turned out that the two references did not refer to the same object; 原来,这两个引用没有引用同一个对象; I proved this in my test class using: 我在我的测试类中证明了这一点:

System.out.println("User objects equal = " + (user == dao.getUser()));

This printed out false. 这打印出来的是假的。 I would expect that every call to an EntityManager using the same key would return the same object reference, and was surprised (and a bit alarmed!) to find out this was not the case. 我希望每次使用相同密钥对EntityManager的调用都会返回相同的对象引用,并且很惊讶(并且有点惊慌!)发现情况并非如此。 Can anyone shed any light on this? 任何人都可以对此有所了解吗? I've refactored my code so it's not actually an issue (the DAO shouldn't have had the User object in it anyway) but I'd still like to understand this better. 我已经重构了我的代码,所以它实际上并不是一个问题(DAO不应该在其中包含User对象)但我仍然希望更好地理解它。

Thanks! 谢谢!

Java 1.6u22, Toplink Essentials 2.0.1, Spring 2.5.6 Java 1.6u22,Toplink Essentials 2.0.1,Spring 2.5.6

find() returns the same instance inside a scope of persistence context . find()持久化上下文范围内返回相同的实例。

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. 对于共享EntityManager (容器管理的事务范围持久化上下文,在JPA Spec术语中),持久性上下文的生命周期绑定到事务,因此当从同一事务调用时, find()返回相同的实例。 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. 我想在您的情况下,您的测试设置不会发生在与测试方法相同的事务中,因此 find()会生成不同的实例。

No it does not. 不,不是的。 You should rely on object EQUALITY instead of IDENTITY anyway. 无论如何,你应该依赖于对象EQUALITY而不是IDENTITY。 Override the equals method. 覆盖equals方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA 2(EclipseLink)尝试使用UUID作为主键EntityManager.find()始终抛出异常(Database is PostgreSQL) - JPA 2 (EclipseLink) Trying to use UUID as primary key EntityManager.find() always throws exception (Database is PostgreSQL) EntityManager.find行为,其中对象包含2个id作为主键 - EntityManager.find behavior with an object containing 2 id as primary key JPA继承entitymanager.find产生ClassCastException - JPA Inheritance entitymanager.find produces ClassCastException 何时将 EntityManager.find() 与 EntityManager.getReference() 与 JPA 一起使用 - When to use EntityManager.find() vs EntityManager.getReference() with JPA 调用entityManager.find()是否需要EntityTransaction? - Does calling entityManager.find() require an EntityTransaction? Jpa 2.0 - EntityManager.find(SomeEntity.class,PK)需要将Descriminator值填入key - Jpa 2.0 - EntityManager.find (SomeEntity.class,PK) need to fill Descriminator value to key jpa-springentitymanager.find超时不起作用 - jpa-spring entitymanager.find timeout not working 为什么EntityManager.merge()防止LazyInitializationException而EntityManager.find()却没有? - Why does EntityManager.merge() prevent LazyInitializationException while EntityManager.find() don't? JPA2(JBoss7.1的Hibernate)entityManager.find()从缓存而不是从数据库获取数据 - JPA2(JBoss7.1's Hibernate) entityManager.find() is getting data from Cache not from DB EntityManager.find方法导致MySQLIntegrityConstraintViolationException - EntityManager.find method leading to MySQLIntegrityConstraintViolationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM