HHH-6043 PostLoad method invoked before collection initialised

This commit is contained in:
brmeyer 2012-09-25 23:33:13 -04:00
parent 121f495ff8
commit 67beb085d3
3 changed files with 137 additions and 0 deletions

View File

@ -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 File

@ -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 File

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