Skip to content
Browse files

HHH-6043 PostLoad method invoked before collection initialised

  • Loading branch information...
1 parent 121f495 commit 67beb085d3379d5be32103be4d1ee1fc9f867b7d brmeyer committed Sep 25, 2012
View
35 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;
+ }
+
+
+}
+
+
View
51 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<Child> children = new HashSet<Child>();
+ 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<Child> getChildren() {
+ return children;
+ }
+ public void setChildren(Set<Child> 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;
+ }
+}
+
View
51 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<Child> children = new HashSet<Child>();
+ 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 };
+ }
+}
+
+

0 comments on commit 67beb08

Please sign in to comment.
Something went wrong with that request. Please try again.