diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazyload/Child.java b/hibernate-core/src/test/java/org/hibernate/test/lazyload/Child.java new file mode 100644 index 0000000000..f45422bae6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/lazyload/Child.java @@ -0,0 +1,43 @@ +package org.hibernate.test.lazyload; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +/** + * @author Oleksander Dukhno + */ + +@Entity +public class Child { + + private Long id; + private Parent parent; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @ManyToOne( + cascade = CascadeType.ALL, + fetch = FetchType.LAZY + ) + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazyload/LazyLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/test/lazyload/LazyLoadingTest.java new file mode 100644 index 0000000000..650357e907 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/lazyload/LazyLoadingTest.java @@ -0,0 +1,94 @@ +package org.hibernate.test.lazyload; + +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * @author Oleksander Dukhno + */ +public class LazyLoadingTest + extends BaseCoreFunctionalTestCase { + + private static final int CHILDREN_SIZE = 3; + private Long parentID; + private Long lastChildID; + + protected void configure(Configuration cfg) { + super.configure( cfg ); + cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); + } + + protected Class[] getAnnotatedClasses() { + return new Class[] { + Parent.class, + Child.class + }; + } + + protected void prepareTest() + throws Exception { + Session s = openSession(); + s.beginTransaction(); + + List children = new ArrayList(); + for ( int i = 0; i < CHILDREN_SIZE; i++ ) { + Child c = new Child(); + s.persist( c ); + lastChildID = c.getId(); + children.add( c ); + } + Parent p = new Parent(); + p.setChildren( children ); + s.persist( p ); + parentID = p.getId(); + + s.getTransaction().commit(); + s.clear(); + s.close(); + } + + @Test + @TestForIssue(jiraKey = "HHH-7971") + public void testLazyCollectionLoadingAfterEndTransaction() { + Session s = openSession(); + s.beginTransaction(); + Parent loadedPatent = (Parent) s.load( Parent.class, parentID ); + s.getTransaction().commit(); + s.close(); + + int i = 0; + for ( Child child : loadedPatent.getChildren() ) { + i++; + assertNotNull( child ); + } + + assertEquals( CHILDREN_SIZE, i ); + + s = openSession(); + s.beginTransaction(); + Child loadedChild = (Child) s.load( Child.class, lastChildID ); + s.getTransaction().commit(); + s.close(); + + Parent p = loadedChild.getParent(); + int j = 0; + for ( Child child : p.getChildren() ) { + j++; + assertNotNull( child ); + } + + assertEquals( CHILDREN_SIZE, j ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazyload/Parent.java b/hibernate-core/src/test/java/org/hibernate/test/lazyload/Parent.java new file mode 100644 index 0000000000..ce93224479 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/lazyload/Parent.java @@ -0,0 +1,43 @@ +package org.hibernate.test.lazyload; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Oleksander Dukhno + */ + +@Entity +public class Parent { + private Long id; + private List children = new ArrayList(); + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + for ( Child c : children ) { + c.setParent( this ); + } + } +}