From 6455107c6ecdd60b0d0daf27f2319c52108a8440 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Mon, 24 Dec 2012 14:53:09 +0100 Subject: [PATCH] HHH-7561 - Fix and test ( from Lukasz ) --- .../tuple/entity/AbstractEntityTuplizer.java | 3 ++ .../org/hibernate/ejb/test/util/Article.java | 44 +++++++++++++++++++ .../ejb/test/util/GetIdentifierTest.java | 40 +++++++++++++++-- 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/Article.java diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java index b0aee6fa61..83239d871e 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java @@ -328,6 +328,9 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) { id = entity; } + else if ( HibernateProxy.class.isInstance( entity ) ) { + id = ( (HibernateProxy) entity ).getHibernateLazyInitializer().getIdentifier(); + } else { if ( idGetter == null ) { if (identifierMapperType==null) { diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/Article.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/Article.java new file mode 100644 index 0000000000..ca5ad4eee4 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/Article.java @@ -0,0 +1,44 @@ +package org.hibernate.ejb.test.util; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +public class Article implements Serializable { + @Id + @GeneratedValue + private Integer id; + + @ManyToOne(fetch = FetchType.LAZY) + private Author author; + + public Article() { + } + + public Article(Author author) { + this.author = author; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java index 2e98adf444..bb7c4fa728 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java @@ -23,13 +23,14 @@ */ package org.hibernate.ejb.test.util; -import javax.persistence.EntityManager; import org.junit.Test; - +import javax.persistence.EntityManager; import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase; - +import org.hibernate.proxy.HibernateProxy; +import org.hibernate.testing.TestForIssue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Emmanuel Bernard @@ -47,6 +48,36 @@ public class GetIdentifierTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + @Test + @TestForIssue(jiraKey = "HHH-7561") + public void testProxyObject() { + EntityManager em = entityManagerFactory().createEntityManager(); + em.getTransaction().begin(); + Book book = new Book(); + em.persist( book ); + em.flush(); + em.clear(); // Clear persistence context to receive proxy object below. + Book proxy = em.getReference( Book.class, book.getId() ); + assertTrue( proxy instanceof HibernateProxy ); + assertEquals( book.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( proxy ) ); + em.getTransaction().rollback(); + em.close(); + + em = entityManagerFactory().createEntityManager(); + em.getTransaction().begin(); + Author author = new Author(); + Article article = new Article( author ); + em.persist( author ); + em.persist( article ); + em.flush(); + em.clear(); // Clear persistence context to receive proxy relation below. + article = em.find( Article.class, article.getId() ); + assertTrue( article.getAuthor() instanceof HibernateProxy ); + assertEquals( author.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( article.getAuthor() ) ); + em.getTransaction().rollback(); + em.close(); + } + @Test public void testEmbeddedId() { EntityManager em = entityManagerFactory().createEntityManager(); @@ -86,7 +117,8 @@ public class GetIdentifierTest extends BaseEntityManagerFunctionalTestCase { Book.class, Umbrella.class, Sickness.class, - Author.class + Author.class, + Article.class }; } }