Skip to content

Commit 87fc258

Browse files
dreab8beikov
authored andcommitted
HHH-16570 Add test for issue
1 parent 6c8bb03 commit 87fc258

File tree

3 files changed

+564
-0
lines changed

3 files changed

+564
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2014 JBoss Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.hibernate.orm.test.batch;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.hibernate.annotations.Fetch;
22+
import org.hibernate.annotations.FetchMode;
23+
import org.hibernate.cfg.AvailableSettings;
24+
25+
import org.hibernate.testing.orm.junit.DomainModel;
26+
import org.hibernate.testing.orm.junit.JiraKey;
27+
import org.hibernate.testing.orm.junit.ServiceRegistry;
28+
import org.hibernate.testing.orm.junit.SessionFactory;
29+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
30+
import org.hibernate.testing.orm.junit.Setting;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Test;
33+
34+
import jakarta.persistence.Column;
35+
import jakarta.persistence.Entity;
36+
import jakarta.persistence.GeneratedValue;
37+
import jakarta.persistence.Id;
38+
import jakarta.persistence.ManyToOne;
39+
import jakarta.persistence.OneToMany;
40+
import jakarta.persistence.Table;
41+
42+
import static org.assertj.core.api.Assertions.assertThat;
43+
44+
@DomainModel(
45+
annotatedClasses = {
46+
BatchAndBagCollectionTest.EntityA.class,
47+
BatchAndBagCollectionTest.EntityB.class
48+
}
49+
)
50+
@ServiceRegistry(
51+
settings = {
52+
@Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10")
53+
}
54+
)
55+
@SessionFactory
56+
@JiraKey("HHH-16570")
57+
public class BatchAndBagCollectionTest {
58+
59+
@BeforeAll
60+
public void setUp(SessionFactoryScope scope) {
61+
scope.inTransaction(
62+
session -> {
63+
EntityA entityA = new EntityA( 1 );
64+
EntityA childA1 = new EntityA( 2 );
65+
EntityA childA2 = new EntityA( 3 );
66+
67+
EntityB entityB1 = new EntityB();
68+
EntityB entityB2 = new EntityB();
69+
EntityB entityB3 = new EntityB();
70+
71+
entityA.addChild( childA1 );
72+
entityA.addChild( childA2 );
73+
74+
childA1.setListOfEntitiesB( List.of( entityB1, entityB2, entityB3 ) );
75+
76+
session.persist( entityA );
77+
session.persist( childA1 );
78+
session.persist( childA2 );
79+
session.persist( entityB1 );
80+
session.persist( entityB2 );
81+
session.persist( entityB3 );
82+
}
83+
);
84+
85+
}
86+
87+
@Test
88+
public void testOneToManyHasCorrectSize(SessionFactoryScope scope) {
89+
scope.inTransaction(
90+
session -> {
91+
List<EntityA> entitiesA = session.createQuery(
92+
"select a from EntityA a where a.parent is null",
93+
EntityA.class
94+
)
95+
.getResultList();
96+
assertThat( entitiesA ).hasSize( 1 );
97+
EntityA entityA = entitiesA.get( 0 );
98+
assertThat( entityA.getId() ).isEqualTo( 1 );
99+
assertThat( entityA.getChildren() ).hasSize( 2 );
100+
}
101+
);
102+
}
103+
104+
@Entity(name = "EntityA")
105+
@Table(name = "ENTITY_A")
106+
public static class EntityA {
107+
@Id
108+
Integer id;
109+
110+
String name;
111+
112+
@ManyToOne
113+
EntityA parent;
114+
115+
@OneToMany(mappedBy = "parent")
116+
List<EntityA> children = new ArrayList<>();
117+
118+
@OneToMany
119+
@Fetch(FetchMode.JOIN)
120+
List<EntityB> listOfEntitiesB = new ArrayList<>();
121+
122+
public EntityA() {
123+
}
124+
125+
public EntityA(Integer id) {
126+
this.id = id;
127+
}
128+
129+
public Integer getId() {
130+
return id;
131+
}
132+
133+
public List<EntityA> getChildren() {
134+
return children;
135+
}
136+
137+
public void setChildren(List<EntityA> children) {
138+
this.children = children;
139+
}
140+
141+
public void setListOfEntitiesB(List<EntityB> listOfEntitiesB) {
142+
this.listOfEntitiesB = listOfEntitiesB;
143+
}
144+
145+
public void addChild(EntityA childA) {
146+
children.add( childA );
147+
childA.parent = this;
148+
}
149+
}
150+
151+
@Entity(name = "EntityB")
152+
@Table(name = "ENTITY_B")
153+
public static class EntityB {
154+
@Id
155+
@GeneratedValue
156+
@Column(name = "ID")
157+
Integer id;
158+
159+
String name;
160+
}
161+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package org.hibernate.orm.test.batch;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import org.hibernate.cfg.AvailableSettings;
9+
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.ServiceRegistry;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.hibernate.testing.orm.junit.Setting;
15+
import org.junit.jupiter.api.BeforeAll;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.FetchType;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.ManyToMany;
22+
import jakarta.persistence.OneToMany;
23+
import jakarta.persistence.Table;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@DomainModel(
28+
annotatedClasses = {
29+
SetAndBagCollectionTest.Customer.class,
30+
SetAndBagCollectionTest.Order.class,
31+
SetAndBagCollectionTest.Item.class,
32+
}
33+
)
34+
@SessionFactory
35+
@ServiceRegistry(
36+
settings = {
37+
@Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10")
38+
}
39+
)
40+
public class SetAndBagCollectionTest {
41+
42+
@BeforeAll
43+
public void setUp(SessionFactoryScope scope) {
44+
scope.inTransaction(
45+
session -> {
46+
Customer customer = new Customer( 1, "First" );
47+
Order order1 = new Order( 1, "First Order" );
48+
Order order2 = new Order( 2, "Second Order" );
49+
50+
customer.addOrder( order1 );
51+
customer.addOrder( order2 );
52+
53+
Item item1 = new Item( 1, "first" );
54+
Item item2 = new Item( 2, "second" );
55+
Item item3 = new Item( 3, "third" );
56+
57+
order1.addItem( item1 );
58+
order1.addItem( item2 );
59+
order1.addItem( item3 );
60+
order1.addItem( item3 );
61+
62+
Item item4 = new Item( 4, "fourth" );
63+
Item item5 = new Item( 5, "fifth" );
64+
65+
order2.addItem( item4 );
66+
order2.addItem( item5 );
67+
68+
session.persist( item1 );
69+
session.persist( item2 );
70+
session.persist( item3 );
71+
session.persist( item4 );
72+
session.persist( item5 );
73+
74+
session.persist( order1 );
75+
session.persist( order2 );
76+
77+
session.persist( customer );
78+
}
79+
);
80+
}
81+
82+
@Test
83+
public void testThatRetrievedBagElementsAreofTheRightCardinality(SessionFactoryScope scope) {
84+
scope.inTransaction(
85+
session -> {
86+
Customer customer = session.get( Customer.class, 1 );
87+
Set<Order> orders = customer.getOrders();
88+
89+
assertThat( orders.size() ).isEqualTo( 2 );
90+
91+
orders.forEach(
92+
order -> {
93+
Collection<Item> items = order.getItems();
94+
if ( order.getId() == 1 ) {
95+
assertThat( items.size() ).isEqualTo( 4 );
96+
}
97+
else {
98+
assertThat( items.size() ).isEqualTo( 2 );
99+
}
100+
}
101+
);
102+
103+
}
104+
);
105+
}
106+
107+
@Entity(name = "Customer")
108+
public static class Customer {
109+
@Id
110+
public Integer id;
111+
112+
public String name;
113+
114+
@OneToMany
115+
Set<Order> orders = new HashSet<>();
116+
117+
public Customer() {
118+
}
119+
120+
public Customer(int id, String name) {
121+
this.id = id;
122+
this.name = name;
123+
}
124+
125+
public Integer getId() {
126+
return id;
127+
}
128+
129+
public String getName() {
130+
return name;
131+
}
132+
133+
public Set<Order> getOrders() {
134+
return orders;
135+
}
136+
137+
public void addOrder(Order order) {
138+
orders.add( order );
139+
}
140+
}
141+
142+
@Entity(name = "Order")
143+
@Table(name = "ORDER_TABLE")
144+
public static class Order {
145+
@Id
146+
public Integer id;
147+
148+
public String description;
149+
150+
@ManyToMany(fetch = FetchType.EAGER)
151+
Collection<Item> items = new ArrayList<>();
152+
153+
public Order() {
154+
}
155+
156+
public Order(Integer id, String description) {
157+
this.id = id;
158+
this.description = description;
159+
}
160+
161+
public Integer getId() {
162+
return id;
163+
}
164+
165+
public String getDescription() {
166+
return description;
167+
}
168+
169+
public Collection<Item> getItems() {
170+
return items;
171+
}
172+
173+
public void addItem(Item item) {
174+
items.add( item );
175+
item.orders.add( this);
176+
}
177+
}
178+
179+
@Entity(name = "Item")
180+
@Table(name = "ITEM_TABLE")
181+
public static class Item {
182+
@Id
183+
public Integer id;
184+
185+
public String description;
186+
187+
@ManyToMany(mappedBy = "items")
188+
public Collection<Order> orders = new ArrayList<>();
189+
190+
public Item() {
191+
}
192+
193+
public Item(Integer id, String description) {
194+
this.id = id;
195+
this.description = description;
196+
}
197+
198+
public Integer getId() {
199+
return id;
200+
}
201+
202+
public String getDescription() {
203+
return description;
204+
}
205+
}
206+
}

0 commit comments

Comments
 (0)