Issue Type: Bug Bug
Affects Versions: 4.1.6
Assignee: Unassigned
Components: entity-manager
Created: 30/Aug/12 6:01 AM
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

<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

@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

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
Project: Hibernate ORM
Priority: Major Major
Reporter: Matt Todd
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira