Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

HHH-6043 PostLoad method invoked before collection initialised

  • Loading branch information...
commit 67beb085d3379d5be32103be4d1ee1fc9f867b7d 1 parent 121f495
Brett Meyer brmeyer authored
35 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Child.java
... ... @@ -0,0 +1,35 @@
  1 +package org.hibernate.jpa.test.collection;
  2 +
  3 +import javax.persistence.Entity;
  4 +import javax.persistence.Id;
  5 +import javax.persistence.ManyToOne;
  6 +
  7 +@Entity
  8 +public class Child {
  9 +
  10 + private Integer id;
  11 + private Parent daddy;
  12 +
  13 + public Child() {
  14 +
  15 + }
  16 +
  17 + @Id
  18 + public Integer getId() {
  19 + return id;
  20 + }
  21 + public void setId(Integer id) {
  22 + this.id = id;
  23 + }
  24 + @ManyToOne
  25 + public Parent getDaddy() {
  26 + return daddy;
  27 + }
  28 + public void setDaddy(Parent daddy) {
  29 + this.daddy = daddy;
  30 + }
  31 +
  32 +
  33 +}
  34 +
  35 +
51 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/Parent.java
... ... @@ -0,0 +1,51 @@
  1 +package org.hibernate.jpa.test.collection;
  2 +
  3 +import java.util.HashSet;
  4 +import java.util.Set;
  5 +
  6 +import javax.persistence.CascadeType;
  7 +import javax.persistence.Entity;
  8 +import javax.persistence.FetchType;
  9 +import javax.persistence.Id;
  10 +import javax.persistence.OneToMany;
  11 +import javax.persistence.PostLoad;
  12 +import javax.persistence.Transient;
  13 +
  14 +@Entity
  15 +public class Parent {
  16 +
  17 + private Integer id;
  18 + private Set<Child> children = new HashSet<Child>();
  19 + private int nrOfChildren;
  20 +
  21 + public Parent() {
  22 +
  23 + }
  24 +
  25 + @Id
  26 + public Integer getId() {
  27 + return id;
  28 + }
  29 + public void setId(Integer id) {
  30 + this.id = id;
  31 + }
  32 + @OneToMany(mappedBy="daddy", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  33 + public Set<Child> getChildren() {
  34 + return children;
  35 + }
  36 + public void setChildren(Set<Child> children) {
  37 + this.children = children;
  38 + //children.size(); // This will result in the same exception as the PostLoad method...
  39 + }
  40 +
  41 + @PostLoad
  42 + public void postLoad() {
  43 + nrOfChildren = children.size();
  44 + }
  45 +
  46 + @Transient
  47 + public int getNrOfChildren() {
  48 + return nrOfChildren;
  49 + }
  50 +}
  51 +
51 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/collection/PostLoadTest.java
... ... @@ -0,0 +1,51 @@
  1 +package org.hibernate.jpa.test.collection;
  2 +
  3 +import static org.junit.Assert.assertEquals;
  4 +
  5 +import java.util.HashSet;
  6 +import java.util.Set;
  7 +
  8 +import javax.persistence.EntityManager;
  9 +
  10 +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
  11 +import org.hibernate.testing.FailureExpected;
  12 +import org.hibernate.testing.TestForIssue;
  13 +import org.junit.Test;
  14 +
  15 +@TestForIssue( jiraKey="HHH-6043" )
  16 +public class PostLoadTest extends BaseEntityManagerFunctionalTestCase {
  17 +
  18 + /**
  19 + * Load an entity with a collection of associated entities, that uses a @PostLoad method to
  20 + * access the association.
  21 + */
  22 + @Test
  23 + @FailureExpected( jiraKey="HHH-6043" )
  24 + public void testAccessAssociatedSetInPostLoad() {
  25 + Child child = new Child();
  26 + child.setId(1);
  27 + Parent daddy = new Parent();
  28 + daddy.setId(1);
  29 + child.setDaddy(daddy);
  30 + Set<Child> children = new HashSet<Child>();
  31 + children.add(child);
  32 + daddy.setChildren(children);
  33 +
  34 + EntityManager em = getOrCreateEntityManager();
  35 +
  36 + em.getTransaction().begin();
  37 + em.persist(daddy);
  38 + em.getTransaction().commit();
  39 + em.clear();
  40 +
  41 + daddy = em.find(Parent.class, 1);
  42 + assertEquals(1, daddy.getNrOfChildren());
  43 + }
  44 +
  45 + @Override
  46 + protected Class<?>[] getAnnotatedClasses() {
  47 + return new Class[] { Child.class, Parent.class };
  48 + }
  49 +}
  50 +
  51 +

0 comments on commit 67beb08

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