Skip to content

Commit 684bad4

Browse files
committed
Various code cleanup
1 parent 4d3e8a9 commit 684bad4

File tree

1 file changed

+124
-110
lines changed

1 file changed

+124
-110
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/nestedfetch/NestedFetchTest.java

Lines changed: 124 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -4,164 +4,178 @@
44
*/
55
package org.hibernate.orm.test.jpa.criteria.nestedfetch;
66

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;
198
import jakarta.persistence.Entity;
20-
import jakarta.persistence.FetchType;
219
import jakarta.persistence.GeneratedValue;
2210
import jakarta.persistence.Id;
23-
import jakarta.persistence.IdClass;
2411
import jakarta.persistence.JoinColumn;
2512
import jakarta.persistence.ManyToOne;
2613
import jakarta.persistence.OneToMany;
27-
import jakarta.persistence.PrimaryKeyJoinColumn;
14+
import jakarta.persistence.Table;
2815
import jakarta.persistence.criteria.CriteriaBuilder;
2916
import jakarta.persistence.criteria.CriteriaQuery;
3017
import jakarta.persistence.criteria.Fetch;
3118
import jakarta.persistence.criteria.JoinType;
3219
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;
3328

34-
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
import java.util.HashSet;
30+
import java.util.Set;
3531

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,
4140
} )
41+
@SessionFactory( useCollectingStatementInspector = true )
4242
@Jira( "https://hibernate.atlassian.net/browse/HHH-16905" )
4343
public class NestedFetchTest {
4444
@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 );
5359
} );
5460
}
5561

5662
@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();
6365
}
6466

6567
@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();
8080
} );
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 );
8791
}
8892

89-
@Entity( name = "RootEntity" )
90-
public static class RootEntity {
93+
@Entity
94+
@Table(name="products")
95+
@SuppressWarnings({"FieldCanBeLocal", "unused"})
96+
public static class Product {
9197
@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;
97100

98-
public Set<Child1Entity> getChild1Entities() {
99-
return child1Entities;
101+
public Product() {
100102
}
101-
}
102103

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+
}
112108
}
113109

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 {
117114
@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<>();
129119

130-
public Child1Entity(RootEntity rootEntity, Child2Entity child2Entity) {
131-
this.rootEntity = rootEntity;
132-
this.child2Entity = child2Entity;
120+
public Customer() {
133121
}
134122

135-
public Child2Entity getChild2Entity() {
136-
return child2Entity;
123+
public Customer(Integer id, String name) {
124+
this.id = id;
125+
this.name = name;
137126
}
138127
}
139128

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 {
143133
@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+
}
151144

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 );
153150
}
154151

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 ) );
157154
}
155+
}
158156

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() {
161173
}
162174

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;
165179
}
166180
}
167181
}

0 commit comments

Comments
 (0)