From d28b3fe9db77efbcb8fc7ef1ed1b9d1ace730f09 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Sun, 19 Jan 2014 14:15:27 +0100 Subject: [PATCH] HHH-8903 Inverse @OneToMany ignored in entity graph --- .../jpa/test/graphs/EntityGraphTest.java | 90 +++++++++++++++++-- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphTest.java index fa9db8f6d4..309d41d7c2 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphTest.java @@ -24,10 +24,12 @@ package org.hibernate.jpa.test.graphs; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import java.util.HashSet; import javax.persistence.Entity; import javax.persistence.EntityGraph; @@ -36,6 +38,7 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.Subgraph; import org.hibernate.Hibernate; @@ -93,7 +96,76 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { em.close(); } - /** + @Test + public void loadCollection() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Bar bar = new Bar(); + em.persist( bar ); + + Foo foo = new Foo(); + foo.bar = bar; + bar.foos.add(foo); + em.persist( foo ); + + em.getTransaction().commit(); + em.clear(); + + em.getTransaction().begin(); + + EntityGraph barGraph = em.createEntityGraph( Bar.class ); + barGraph.addAttributeNodes("foos"); + + Map properties = new HashMap(); + properties.put( "javax.persistence.loadgraph", barGraph); + + Bar result = em.find( Bar.class, bar.id, properties ); + + assertTrue( Hibernate.isInitialized( result ) ); + assertTrue( Hibernate.isInitialized( result.foos ) ); + + em.getTransaction().commit(); + em.close(); + } + + @Test + public void loadInverseCollection() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Bar bar = new Bar(); + em.persist( bar ); + + Foo foo = new Foo(); + foo.bar = bar; + bar.foos.add(foo); + em.persist( foo ); + + em.getTransaction().commit(); + em.clear(); + + em.getTransaction().begin(); + + EntityGraph fooGraph = em.createEntityGraph( Foo.class ); + fooGraph.addAttributeNodes("bar"); + Subgraph barGraph = fooGraph.addSubgraph("bar"); + barGraph.addAttributeNodes("foos"); + + Map properties = new HashMap(); + properties.put( "javax.persistence.loadgraph", fooGraph ); + + Foo result = em.find( Foo.class, foo.id, properties ); + + assertTrue( Hibernate.isInitialized( result ) ); + assertTrue( Hibernate.isInitialized( result.bar ) ); + assertTrue( Hibernate.isInitialized( result.bar.foos) ); + + em.getTransaction().commit(); + em.close(); + } + + /** * JPA 2.1 spec: "Add a node to the graph that corresponds to a managed type with inheritance. This allows for * multiple subclass subgraphs to be defined for this node of the entity graph. Subclass subgraphs will * automatically include the specified attributes of superclass subgraphs." @@ -146,7 +218,7 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { } @Entity - private static class Foo { + public static class Foo { @Id @GeneratedValue @@ -160,19 +232,23 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { } @Entity - private static class Bar { + public static class Bar { @Id @GeneratedValue - private Integer id; + public Integer id; + + @OneToMany(mappedBy = "bar") + public Set foos = new HashSet(); } @Entity - private static class Baz { + public static class Baz { @Id @GeneratedValue - private Integer id; + public Integer id; + } }