diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Child.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Child.java new file mode 100644 index 0000000000..2fb2142b6c --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Child.java @@ -0,0 +1,35 @@ +package org.hibernate.jpa.test.collection; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Child { + + private Integer id; + private Parent daddy; + + public Child() { + + } + + @Id + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + @ManyToOne + public Parent getDaddy() { + return daddy; + } + public void setDaddy(Parent daddy) { + this.daddy = daddy; + } + + +} + + diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Parent.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Parent.java new file mode 100644 index 0000000000..778d5976a7 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Parent.java @@ -0,0 +1,51 @@ +package org.hibernate.jpa.test.collection; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.PostLoad; +import javax.persistence.Transient; + +@Entity +public class Parent { + + private Integer id; + private Set children = new HashSet(); + private int nrOfChildren; + + public Parent() { + + } + + @Id + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + @OneToMany(mappedBy="daddy", fetch=FetchType.EAGER, cascade=CascadeType.ALL) + public Set getChildren() { + return children; + } + public void setChildren(Set children) { + this.children = children; + //children.size(); // This will result in the same exception as the PostLoad method... + } + + @PostLoad + public void postLoad() { + nrOfChildren = children.size(); + } + + @Transient + public int getNrOfChildren() { + return nrOfChildren; + } +} + diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/PostLoadTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/PostLoadTest.java new file mode 100644 index 0000000000..05edf063df --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/PostLoadTest.java @@ -0,0 +1,51 @@ +package org.hibernate.jpa.test.collection; + +import static org.junit.Assert.assertEquals; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.EntityManager; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +@TestForIssue( jiraKey="HHH-6043" ) +public class PostLoadTest extends BaseEntityManagerFunctionalTestCase { + + /** + * Load an entity with a collection of associated entities, that uses a @PostLoad method to + * access the association. + */ + @Test + @FailureExpected( jiraKey="HHH-6043" ) + public void testAccessAssociatedSetInPostLoad() { + Child child = new Child(); + child.setId(1); + Parent daddy = new Parent(); + daddy.setId(1); + child.setDaddy(daddy); + Set children = new HashSet(); + children.add(child); + daddy.setChildren(children); + + EntityManager em = getOrCreateEntityManager(); + + em.getTransaction().begin(); + em.persist(daddy); + em.getTransaction().commit(); + em.clear(); + + daddy = em.find(Parent.class, 1); + assertEquals(1, daddy.getNrOfChildren()); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Child.class, Parent.class }; + } +} + +