|
6 | 6 | */
|
7 | 7 | package org.hibernate.jpa.test.graphs;
|
8 | 8 |
|
| 9 | +import javax.persistence.AttributeNode; |
9 | 10 | import javax.persistence.Entity;
|
10 | 11 | import javax.persistence.EntityGraph;
|
11 | 12 | import javax.persistence.EntityManager;
|
12 | 13 | import javax.persistence.GeneratedValue;
|
13 |
| -import javax.persistence.GenerationType; |
14 | 14 | import javax.persistence.Id;
|
15 | 15 | import javax.persistence.JoinColumn;
|
16 | 16 | import javax.persistence.ManyToOne;
|
|
20 | 20 | import javax.persistence.Temporal;
|
21 | 21 | import javax.persistence.TemporalType;
|
22 | 22 | import javax.persistence.TypedQuery;
|
| 23 | +import javax.persistence.metamodel.Attribute; |
23 | 24 |
|
| 25 | +import java.util.Collection; |
24 | 26 | import java.util.Date;
|
25 | 27 | import java.util.List;
|
26 | 28 |
|
27 | 29 | import org.hibernate.Hibernate;
|
28 | 30 | import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
| 31 | +import org.hibernate.metamodel.model.domain.spi.EntityTypeDescriptor; |
29 | 32 | import org.hibernate.testing.TestForIssue;
|
30 | 33 | import org.junit.Test;
|
31 | 34 |
|
| 35 | +import static org.junit.Assert.assertEquals; |
32 | 36 | import static org.junit.Assert.assertTrue;
|
33 | 37 |
|
34 | 38 | /**
|
@@ -93,7 +97,7 @@ public void fetchSubGraphFromSubgraph() {
|
93 | 97 |
|
94 | 98 | @Test
|
95 | 99 | @TestForIssue( jiraKey = "HHH-9392")
|
96 |
| - public void fetchAttributeNodeFromSubgraph() { |
| 100 | + public void fetchAttributeNodeByStringFromSubgraph() { |
97 | 101 | EntityManager em = getOrCreateEntityManager();
|
98 | 102 | em.getTransaction().begin();
|
99 | 103 |
|
@@ -136,6 +140,70 @@ public void fetchAttributeNodeFromSubgraph() {
|
136 | 140 | query.setHint( "javax.persistence.loadgraph", entityGraph );
|
137 | 141 | final List<CustomerOrder> results = query.getResultList();
|
138 | 142 |
|
| 143 | + assertEntityGraph( entityGraph ); |
| 144 | + assertTrue( Hibernate.isInitialized( results ) ); |
| 145 | + |
| 146 | + em.getTransaction().commit(); |
| 147 | + em.close(); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + @TestForIssue( jiraKey = "HHH-13233") |
| 152 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 153 | + public void fetchAttributeNodeByAttributeFromSubgraph() { |
| 154 | + EntityManager em = getOrCreateEntityManager(); |
| 155 | + em.getTransaction().begin(); |
| 156 | + |
| 157 | + Address address = new Address(); |
| 158 | + address.city = "TestCity"; |
| 159 | + |
| 160 | + CustomerOrder customerOrder = new CustomerOrder(); |
| 161 | + customerOrder.shippingAddress = address; |
| 162 | + |
| 163 | + Product product = new Product(); |
| 164 | + |
| 165 | + OrderPosition orderPosition = new OrderPosition(); |
| 166 | + orderPosition.product = product; |
| 167 | + |
| 168 | + customerOrder.orderPosition = orderPosition; |
| 169 | + em.persist( address ); |
| 170 | + em.persist( orderPosition ); |
| 171 | + em.persist( product ); |
| 172 | + em.persist( customerOrder ); |
| 173 | + |
| 174 | + em.getTransaction().commit(); |
| 175 | + em.clear(); |
| 176 | + |
| 177 | + em.getTransaction().begin(); |
| 178 | + |
| 179 | + final EntityGraph<CustomerOrder> entityGraph = em.createEntityGraph( CustomerOrder.class ); |
| 180 | + EntityTypeDescriptor<CustomerOrder> customerOrderEntityType = |
| 181 | + entityManagerFactory().getMetamodel().entity( CustomerOrder.class ); |
| 182 | + entityGraph.addAttributeNodes( |
| 183 | + (Attribute) customerOrderEntityType.getAttribute( "shippingAddress" ), |
| 184 | + (Attribute) customerOrderEntityType.getAttribute( "orderDate" ) |
| 185 | + ); |
| 186 | + entityGraph.addAttributeNodes( (Attribute) customerOrderEntityType.getAttribute( "shippingAddress" ) ); |
| 187 | + |
| 188 | + final Subgraph<OrderPosition> orderProductsSubgraph = |
| 189 | + entityGraph.addSubgraph( (Attribute) customerOrderEntityType.getAttribute( "orderPosition" ) ); |
| 190 | + EntityTypeDescriptor<OrderPosition> positionEntityType = |
| 191 | + entityManagerFactory().getMetamodel().entity( OrderPosition.class ); |
| 192 | + orderProductsSubgraph.addAttributeNodes( (Attribute) positionEntityType.getAttribute( "amount" ) ); |
| 193 | + orderProductsSubgraph.addAttributeNodes( (Attribute) positionEntityType.getAttribute( "product" ) ); |
| 194 | + |
| 195 | + final Subgraph<Product> productSubgraph = |
| 196 | + orderProductsSubgraph.addSubgraph( (Attribute) positionEntityType.getAttribute( "product" ) ); |
| 197 | + EntityTypeDescriptor<Product> productEntityType = entityManagerFactory().getMetamodel().entity( Product.class ); |
| 198 | + productSubgraph.addAttributeNodes( (Attribute) productEntityType.getAttribute( "productName" ) ); |
| 199 | + |
| 200 | + TypedQuery<CustomerOrder> query = em.createQuery( |
| 201 | + "SELECT o FROM EntityGraphUsingFetchGraphTest$CustomerOrder o", CustomerOrder.class |
| 202 | + ); |
| 203 | + query.setHint( "javax.persistence.loadgraph", entityGraph ); |
| 204 | + final List<CustomerOrder> results = query.getResultList(); |
| 205 | + |
| 206 | + assertEntityGraph( entityGraph ); |
139 | 207 | assertTrue( Hibernate.isInitialized( results ) );
|
140 | 208 |
|
141 | 209 | em.getTransaction().commit();
|
@@ -184,6 +252,38 @@ public void fetchUsingHql() {
|
184 | 252 | em.close();
|
185 | 253 | }
|
186 | 254 |
|
| 255 | + |
| 256 | + /** |
| 257 | + * Verify that entityGraph has expected state |
| 258 | + * |
| 259 | + * customerOrder - shippingAddress |
| 260 | + * - orderDate |
| 261 | + * - orderPosition - amount |
| 262 | + * - product - productName |
| 263 | + * |
| 264 | + * @param entityGraph entityGraph |
| 265 | + */ |
| 266 | + private void assertEntityGraph(EntityGraph<CustomerOrder> entityGraph) { |
| 267 | + assertEquals(3, entityGraph.getAttributeNodes().size()); |
| 268 | + for ( AttributeNode<?> entityGraphAttributeNode : entityGraph.getAttributeNodes() ) { |
| 269 | + if ( "orderPosition".equals( entityGraphAttributeNode.getAttributeName() ) ) { |
| 270 | + Collection<Subgraph> orderPositionGraph = entityGraphAttributeNode.getSubgraphs().values(); |
| 271 | + assertEquals( 1, orderPositionGraph.size() ); |
| 272 | + List<AttributeNode<?>> orderPositionAttributes = orderPositionGraph.iterator().next().getAttributeNodes(); |
| 273 | + assertEquals( 2, orderPositionAttributes.size() ); |
| 274 | + for ( AttributeNode<?> orderPositionAttributeNode : orderPositionAttributes ) { |
| 275 | + if ( "product".equals( orderPositionAttributeNode.getAttributeName() ) ) { |
| 276 | + assertEquals( 1, orderPositionAttributeNode.getSubgraphs().size() ); |
| 277 | + } else { |
| 278 | + assertTrue( orderPositionAttributeNode.getSubgraphs().isEmpty() ); |
| 279 | + } |
| 280 | + } |
| 281 | + } else { |
| 282 | + assertTrue( entityGraphAttributeNode.getSubgraphs().isEmpty() ); |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + |
187 | 287 | @Entity
|
188 | 288 | @Table(name = "customerOrder")
|
189 | 289 | public static class CustomerOrder {
|
|
0 commit comments