From 8e758fcd6b6bf81faa5df713f5aa935f6b172478 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 11 Mar 2015 22:03:06 -0700 Subject: [PATCH] HHH-9392 : SQLGrammarException while executing a entity graph with subgraphs (test case) --- .../EntityGraphUsingFetchGraphTest.java | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphUsingFetchGraphTest.java diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphUsingFetchGraphTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphUsingFetchGraphTest.java new file mode 100644 index 0000000000..ebb2720c06 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/EntityGraphUsingFetchGraphTest.java @@ -0,0 +1,205 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2015, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc.. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.jpa.test.graphs; + +import javax.persistence.Entity; +import javax.persistence.EntityGraph; +import javax.persistence.EntityManager; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; +import javax.persistence.Subgraph; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.TypedQuery; +import java.util.Date; +import java.util.List; + +import org.hibernate.Hibernate; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * @author Baris Cubukcuoglu + */ +public class EntityGraphUsingFetchGraphTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {CustomerOrder.class, OrderPosition.class, Product.class, Address.class}; + } + + @Test + public void fetchSubGraphFromSubgraph() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Address address = new Address(); + address.city = "TestCity"; + + CustomerOrder customerOrder = new CustomerOrder(); + customerOrder.shippingAddress = address; + + Product product = new Product(); + + OrderPosition orderPosition = new OrderPosition(); + orderPosition.product = product; + + customerOrder.orderPosition = orderPosition; + em.persist( address ); + em.persist( orderPosition ); + em.persist( product ); + em.persist( customerOrder ); + + em.getTransaction().commit(); + em.clear(); + + em.getTransaction().begin(); + + final EntityGraph entityGraph = em.createEntityGraph( CustomerOrder.class ); + //entityGraph.addAttributeNodes( "shippingAddress", "orderDate" ); + entityGraph.addAttributeNodes( "shippingAddress" ); + + final Subgraph orderProductsSubgraph = entityGraph.addSubgraph( "orderPosition" ); + //orderProductsSubgraph.addAttributeNodes( "amount" ); + + final Subgraph productSubgraph = orderProductsSubgraph.addSubgraph( "product" ); + //productSubgraph.addAttributeNodes( "productName" ); + + TypedQuery query = em.createQuery( + "SELECT o FROM EntityGraphUsingFetchGraphTest$CustomerOrder o", CustomerOrder.class + ); + query.setHint( "javax.persistence.loadgraph", entityGraph ); + final List results = query.getResultList(); + + assertTrue( Hibernate.isInitialized( results ) ); + + em.getTransaction().commit(); + em.close(); + } + + @Test + public void fetchAttributeNodeFromSubgraph() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Address address = new Address(); + address.city = "TestCity"; + + CustomerOrder customerOrder = new CustomerOrder(); + customerOrder.shippingAddress = address; + + Product product = new Product(); + + OrderPosition orderPosition = new OrderPosition(); + orderPosition.product = product; + + customerOrder.orderPosition = orderPosition; + em.persist( address ); + em.persist( orderPosition ); + em.persist( product ); + em.persist( customerOrder ); + + em.getTransaction().commit(); + em.clear(); + + em.getTransaction().begin(); + + final EntityGraph entityGraph = em.createEntityGraph( CustomerOrder.class ); + //entityGraph.addAttributeNodes( "shippingAddress", "orderDate" ); + entityGraph.addAttributeNodes( "shippingAddress" ); + + final Subgraph orderProductsSubgraph = entityGraph.addSubgraph( "orderPosition" ); + //orderProductsSubgraph.addAttributeNodes( "amount" ); + orderProductsSubgraph.addAttributeNodes( "product" ); + + //final Subgraph productSubgraph = orderProductsSubgraph.addSubgraph( "product" ); + //productSubgraph.addAttributeNodes( "productName" ); + + TypedQuery query = em.createQuery( + "SELECT o FROM EntityGraphUsingFetchGraphTest$CustomerOrder o", CustomerOrder.class + ); + query.setHint( "javax.persistence.loadgraph", entityGraph ); + final List results = query.getResultList(); + + assertTrue( Hibernate.isInitialized( results ) ); + + em.getTransaction().commit(); + em.close(); + } + + @Entity + public static class CustomerOrder { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + @OneToOne + public OrderPosition orderPosition; + + @Temporal(TemporalType.TIMESTAMP) + public Date orderDate; + + @OneToOne + public Address shippingAddress; + } + + @Entity + public static class Address { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + public String city; + } + + @Entity + public static class OrderPosition { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + public Integer amount; + + @ManyToOne + @JoinColumn(name = "product") + public Product product; + } + + @Entity + public static class Product { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + public String productName; + } +}