|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.jpa.criteria.nestedfetch; |
6 | 6 |
|
7 | | -import java.io.Serializable; |
8 | | -import java.util.HashSet; |
9 | | -import java.util.Set; |
10 | | - |
11 | | -import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
12 | | -import org.hibernate.testing.orm.junit.Jira; |
13 | | -import org.hibernate.testing.orm.junit.Jpa; |
14 | | -import org.junit.jupiter.api.AfterAll; |
15 | | -import org.junit.jupiter.api.BeforeAll; |
16 | | -import org.junit.jupiter.api.Test; |
17 | | - |
18 | | -import jakarta.persistence.Embeddable; |
| 7 | +import jakarta.persistence.CascadeType; |
19 | 8 | import jakarta.persistence.Entity; |
20 | | -import jakarta.persistence.FetchType; |
21 | 9 | import jakarta.persistence.GeneratedValue; |
22 | 10 | import jakarta.persistence.Id; |
23 | | -import jakarta.persistence.IdClass; |
24 | 11 | import jakarta.persistence.JoinColumn; |
25 | 12 | import jakarta.persistence.ManyToOne; |
26 | 13 | import jakarta.persistence.OneToMany; |
27 | | -import jakarta.persistence.PrimaryKeyJoinColumn; |
| 14 | +import jakarta.persistence.Table; |
28 | 15 | import jakarta.persistence.criteria.CriteriaBuilder; |
29 | 16 | import jakarta.persistence.criteria.CriteriaQuery; |
30 | 17 | import jakarta.persistence.criteria.Fetch; |
31 | 18 | import jakarta.persistence.criteria.JoinType; |
32 | 19 | import jakarta.persistence.criteria.Root; |
| 20 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 21 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 22 | +import org.hibernate.testing.orm.junit.Jira; |
| 23 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 24 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
33 | 28 |
|
34 | | -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Set; |
35 | 31 |
|
36 | | -@Jpa( annotatedClasses = { |
37 | | - NestedFetchTest.RootEntity.class, |
38 | | - NestedFetchTest.Chil1PK.class, |
39 | | - NestedFetchTest.Child1Entity.class, |
40 | | - NestedFetchTest.Child2Entity.class, |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + NestedFetchTest.Product.class, |
| 37 | + NestedFetchTest.Customer.class, |
| 38 | + NestedFetchTest.Order.class, |
| 39 | + NestedFetchTest.OrderLine.class, |
41 | 40 | } ) |
| 41 | +@SessionFactory( useCollectingStatementInspector = true ) |
42 | 42 | @Jira( "https://hibernate.atlassian.net/browse/HHH-16905" ) |
43 | 43 | public class NestedFetchTest { |
44 | 44 | @BeforeAll |
45 | | - public void setUp(EntityManagerFactoryScope scope) { |
46 | | - scope.inTransaction( entityManager -> { |
47 | | - final RootEntity root = new RootEntity(); |
48 | | - entityManager.persist( root ); |
49 | | - final Child2Entity child2 = new Child2Entity( "222" ); |
50 | | - entityManager.persist( child2 ); |
51 | | - final Child1Entity child1 = new Child1Entity( root, child2 ); |
52 | | - entityManager.persist( child1 ); |
| 45 | + public void createTestData(SessionFactoryScope scope) { |
| 46 | + scope.inTransaction( (session) -> { |
| 47 | + final Product sprocket = new Product( 1, "Sprocket" ); |
| 48 | + final Product thingamajig = new Product( 2, "Thingamajig" ); |
| 49 | + session.persist( sprocket ); |
| 50 | + session.persist( thingamajig ); |
| 51 | + |
| 52 | + final Customer ibm = new Customer( 1, "IBM" ); |
| 53 | + session.persist( ibm ); |
| 54 | + |
| 55 | + final Order order = new Order( 1, "ibm-1", ibm ); |
| 56 | + order.addOrderLine( sprocket, 20 ); |
| 57 | + order.addOrderLine( thingamajig, 1500 ); |
| 58 | + session.persist( order ); |
53 | 59 | } ); |
54 | 60 | } |
55 | 61 |
|
56 | 62 | @AfterAll |
57 | | - public void tearDown(EntityManagerFactoryScope scope) { |
58 | | - scope.inTransaction( entityManager -> { |
59 | | - entityManager.createQuery( "delete from Child1Entity" ).executeUpdate(); |
60 | | - entityManager.createQuery( "delete from Child2Entity" ).executeUpdate(); |
61 | | - entityManager.createQuery( "delete from RootEntity" ).executeUpdate(); |
62 | | - } ); |
| 63 | + public void tearDown(SessionFactoryScope scope) { |
| 64 | + scope.dropData(); |
63 | 65 | } |
64 | 66 |
|
65 | 67 | @Test |
66 | | - public void testNestedFetch(EntityManagerFactoryScope scope) { |
67 | | - RootEntity rootEntity = scope.fromTransaction( entityManager -> { |
68 | | - final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
69 | | - final CriteriaQuery<RootEntity> cq = cb.createQuery( RootEntity.class ); |
70 | | - final Root<RootEntity> fromRoot = cq.from( RootEntity.class ); |
71 | | - final Fetch<RootEntity, Child1Entity> fetchDepth1 = fromRoot.fetch( |
72 | | - "child1Entities", |
73 | | - JoinType.LEFT |
74 | | - ); |
75 | | - final Fetch<Child1Entity, Child2Entity> fetchDepth2 = fetchDepth1.fetch( |
76 | | - "child2Entity", |
77 | | - JoinType.LEFT |
78 | | - ); |
79 | | - return entityManager.createQuery( cq.select( fromRoot ) ).getSingleResult(); |
| 68 | + public void testNestedFetch(SessionFactoryScope scope) { |
| 69 | + final SQLStatementInspector sqlCollector = scope.getCollectingStatementInspector(); |
| 70 | + sqlCollector.clear(); |
| 71 | + |
| 72 | + final Customer customer = scope.fromTransaction( (session) -> { |
| 73 | + final CriteriaBuilder cb = session.getCriteriaBuilder(); |
| 74 | + final CriteriaQuery<Customer> criteria = cb.createQuery( Customer.class ); |
| 75 | + final Root<Customer> fromRoot = criteria.from( Customer.class ); |
| 76 | + final Fetch<Customer, Order> ordersFetch = fromRoot.fetch( "orders", JoinType.LEFT ); |
| 77 | + final Fetch<Order, OrderLine> linesFetch = ordersFetch.fetch( "lines", JoinType.LEFT ); |
| 78 | + final Fetch<OrderLine, Product> productFetch = linesFetch.fetch( "product", JoinType.LEFT ); |
| 79 | + return session.createQuery( criteria.select( fromRoot ) ).getSingleResult(); |
80 | 80 | } ); |
81 | | - //dependent relations should already be fetched and be available outside the transaction |
82 | | - assertThat( rootEntity.getChild1Entities().size() ).isEqualTo( 1 ); |
83 | | - final Child1Entity depth1Entity = rootEntity.getChild1Entities().iterator().next(); |
84 | | - final Child2Entity depth2Entity = depth1Entity.getChild2Entity(); |
85 | | - assertThat( depth2Entity ).isNotNull(); |
86 | | - assertThat( depth2Entity.getVal() ).isEqualTo( "222" ); |
| 81 | + |
| 82 | + // make sure that the fetches really got fetched... |
| 83 | + assertThat( customer.orders ).hasSize( 1 ); |
| 84 | + final Order order = customer.orders.iterator().next(); |
| 85 | + assertThat( order.lines ).hasSize( 2 ); |
| 86 | + assertThat( order.lines.stream().map( orderLine -> orderLine.product.name ) ) |
| 87 | + .contains( "Sprocket", "Thingamajig" ); |
| 88 | + |
| 89 | + // should have happened by joins |
| 90 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 ); |
87 | 91 | } |
88 | 92 |
|
89 | | - @Entity( name = "RootEntity" ) |
90 | | - public static class RootEntity { |
| 93 | + @Entity |
| 94 | + @Table(name="products") |
| 95 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 96 | + public static class Product { |
91 | 97 | @Id |
92 | | - @GeneratedValue |
93 | | - private int id; |
94 | | - |
95 | | - @OneToMany( mappedBy = "rootEntity" ) |
96 | | - private Set<Child1Entity> child1Entities; |
| 98 | + private Integer id; |
| 99 | + private String name; |
97 | 100 |
|
98 | | - public Set<Child1Entity> getChild1Entities() { |
99 | | - return child1Entities; |
| 101 | + public Product() { |
100 | 102 | } |
101 | | - } |
102 | 103 |
|
103 | | - @Embeddable |
104 | | - public static class Chil1PK implements Serializable { |
105 | | - @ManyToOne( fetch = FetchType.LAZY ) |
106 | | - @JoinColumn( name = "rootId", referencedColumnName = "id", nullable = false ) |
107 | | - private RootEntity rootEntity; |
108 | | - |
109 | | - @ManyToOne( fetch = FetchType.LAZY ) |
110 | | - @JoinColumn( name = "child2Id", referencedColumnName = "id", nullable = false ) |
111 | | - private Child2Entity child2Entity; |
| 104 | + public Product(Integer id, String name) { |
| 105 | + this.id = id; |
| 106 | + this.name = name; |
| 107 | + } |
112 | 108 | } |
113 | 109 |
|
114 | | - @Entity( name = "Child1Entity" ) |
115 | | - @IdClass( Chil1PK.class ) |
116 | | - public static class Child1Entity { |
| 110 | + @Entity |
| 111 | + @Table(name="customers") |
| 112 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 113 | + public static class Customer { |
117 | 114 | @Id |
118 | | - @PrimaryKeyJoinColumn( name = "rootId", referencedColumnName = "id" ) |
119 | | - @ManyToOne( fetch = FetchType.LAZY ) |
120 | | - private RootEntity rootEntity; |
121 | | - |
122 | | - @Id |
123 | | - @PrimaryKeyJoinColumn( name = "child2Id", referencedColumnName = "id" ) |
124 | | - @ManyToOne( fetch = FetchType.LAZY ) |
125 | | - private Child2Entity child2Entity; |
126 | | - |
127 | | - public Child1Entity() { |
128 | | - } |
| 115 | + private Integer id; |
| 116 | + private String name; |
| 117 | + @OneToMany( mappedBy = "customer" ) |
| 118 | + private Set<Order> orders = new HashSet<>(); |
129 | 119 |
|
130 | | - public Child1Entity(RootEntity rootEntity, Child2Entity child2Entity) { |
131 | | - this.rootEntity = rootEntity; |
132 | | - this.child2Entity = child2Entity; |
| 120 | + public Customer() { |
133 | 121 | } |
134 | 122 |
|
135 | | - public Child2Entity getChild2Entity() { |
136 | | - return child2Entity; |
| 123 | + public Customer(Integer id, String name) { |
| 124 | + this.id = id; |
| 125 | + this.name = name; |
137 | 126 | } |
138 | 127 | } |
139 | 128 |
|
140 | | - |
141 | | - @Entity( name = "Child2Entity" ) |
142 | | - public static class Child2Entity { |
| 129 | + @Entity |
| 130 | + @Table(name="orders") |
| 131 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 132 | + public static class Order { |
143 | 133 | @Id |
144 | | - @GeneratedValue |
145 | | - private int id; |
146 | | - |
147 | | - @OneToMany( mappedBy = "child2Entity" ) |
148 | | - private Set<Child1Entity> child1Entities = new HashSet<>(); |
149 | | - |
150 | | - private String val; |
| 134 | + private Integer id; |
| 135 | + private String orderNumber; |
| 136 | + @ManyToOne |
| 137 | + @JoinColumn(name = "customer_fk") |
| 138 | + private Customer customer; |
| 139 | + @OneToMany( mappedBy = "order", cascade = CascadeType.ALL) |
| 140 | + private Set<OrderLine> lines = new HashSet<>(); |
| 141 | + |
| 142 | + public Order() { |
| 143 | + } |
151 | 144 |
|
152 | | - public Child2Entity() { |
| 145 | + public Order(Integer id, String orderNumber, Customer customer) { |
| 146 | + this.id = id; |
| 147 | + this.orderNumber = orderNumber; |
| 148 | + this.customer = customer; |
| 149 | + customer.orders.add( this ); |
153 | 150 | } |
154 | 151 |
|
155 | | - public Child2Entity(String val) { |
156 | | - this.val = val; |
| 152 | + public void addOrderLine(Product product, int quantity) { |
| 153 | + lines.add( new OrderLine( this, product, quantity ) ); |
157 | 154 | } |
| 155 | + } |
158 | 156 |
|
159 | | - public String getVal() { |
160 | | - return val; |
| 157 | + @Entity(name="OrderLine") |
| 158 | + @Table(name="OrderLine") |
| 159 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 160 | + public static class OrderLine { |
| 161 | + @Id |
| 162 | + @GeneratedValue |
| 163 | + private Integer id; |
| 164 | + @ManyToOne |
| 165 | + @JoinColumn(name="order_fk") |
| 166 | + private Order order; |
| 167 | + @ManyToOne |
| 168 | + @JoinColumn(name="product_fk") |
| 169 | + private Product product; |
| 170 | + private int quantity; |
| 171 | + |
| 172 | + public OrderLine() { |
161 | 173 | } |
162 | 174 |
|
163 | | - public Set<Child1Entity> getChild1Entities() { |
164 | | - return child1Entities; |
| 175 | + public OrderLine(Order order, Product product, int quantity) { |
| 176 | + this.order = order; |
| 177 | + this.product = product; |
| 178 | + this.quantity = quantity; |
165 | 179 | } |
166 | 180 | } |
167 | 181 | } |
0 commit comments