PersistenceUnitUtil.getIdentifier returning null for a persisted proxy

Description

When using the PersistenceUnitUtil.getIdentifier method to get the id back from a persisted entity, I have found that it returns null if the entity is a proxy. When accessing the id property of the proxy entity, this has been set correctly. It would appear that the implementation of this method is not correct. Please see the test case described below:

persistence.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <!-- jdbc connection --> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="javax.persistence.jdbc.user" value="sa"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence>

The Entity

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Entity public class EntityBean { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long id; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) private EntityBean child; public Long getId() { return id; } public EntityBean getChild() { return child; } public void setChild(EntityBean child) { this.child = child; } }

The Test case

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public class EntityBeanTest { private PersistenceUnitUtil persistenceUnitUtil; private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private EntityTransaction entityTransaction; private EntityBean parent; @Test public void checkEntityIdFromProxy() { EntityBean child = parent.getChild(); assertFalse(persistenceUnitUtil.isLoaded(child)); assertEquals(child.getId(), persistenceUnitUtil.getIdentifier(child)); } @Before public void setUp() { entityManagerFactory = Persistence.createEntityManagerFactory("manager1"); persistenceUnitUtil = entityManagerFactory.getPersistenceUnitUtil(); entityManager = entityManagerFactory.createEntityManager(); entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); // create an entity bean EntityBean entityBean = new EntityBean(); entityBean.setChild(new EntityBean()); entityManager.persist(entityBean); entityManager.flush(); // get the id of the parent entity Long id = entityBean.getId(); // clear the first level cache entityManager.clear(); // bring the parent back parent = entityManager.find(EntityBean.class, id); } @After public void tearDown() { entityTransaction.commit(); entityManager.close(); entityManagerFactory.close(); } }

Environment

4.1.x & HSQL, Postgresql, SQLServer

Status

Assignee

Lukasz Antoniak

Reporter

Matt Todd

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.1.6

Priority

Major