HHH-7561 - Fix and test ( from Lukasz )

This commit is contained in:
Lukasz Antoniak 2012-12-24 14:53:09 +01:00 committed by Brett Meyer
parent de4517b0d5
commit 6455107c6e
3 changed files with 83 additions and 4 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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
};
}
}