Skip to content

Commit 10ab8cd

Browse files
committed
[hibernate#1867] Test case
1 parent 9089e5b commit 10ab8cd

File tree

2 files changed

+508
-67
lines changed

2 files changed

+508
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Set;
14+
import java.util.concurrent.CompletionStage;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
import io.vertx.junit5.VertxTestContext;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.GeneratedValue;
21+
import jakarta.persistence.GenerationType;
22+
import jakarta.persistence.Id;
23+
import jakarta.persistence.IdClass;
24+
import jakarta.persistence.Inheritance;
25+
import jakarta.persistence.InheritanceType;
26+
import jakarta.persistence.ManyToOne;
27+
import jakarta.persistence.OneToMany;
28+
import jakarta.persistence.SequenceGenerator;
29+
import jakarta.persistence.Table;
30+
31+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
32+
33+
public class ManyToManyWithCompositeIdTest extends BaseReactiveTest {
34+
35+
@Override
36+
protected Collection<Class<?>> annotatedEntities() {
37+
return List.of( CarsClients.class, ClientA.class, Client.class, Car.class );
38+
}
39+
40+
@Override
41+
protected CompletionStage<Void> cleanDb() {
42+
return voidFuture();
43+
}
44+
45+
@Test
46+
public void test(VertxTestContext context) {
47+
List<Client> clients = new ArrayList<>();
48+
for ( int i = 0; i < 5; i++ ) {
49+
ClientA client = new ClientA();
50+
client.setName( "name" + i );
51+
client.setEmail( "email" + i );
52+
client.setPhone( "phone" + i );
53+
clients.add( client );
54+
}
55+
56+
List<Car> cars = new ArrayList<>();
57+
for ( int i = 0; i < 2; i++ ) {
58+
Car car = new Car();
59+
car.setBrand( "brand" + i );
60+
car.setModel( "model" + i );
61+
cars.add( car );
62+
}
63+
64+
test( context, getMutinySessionFactory()
65+
.withSession( session -> {
66+
session.setBatchSize( 5 );
67+
return session.persistAll( cars.toArray() )
68+
.chain( () -> session
69+
.persistAll( clients.toArray() )
70+
.chain( session::flush ) )
71+
.chain( () -> {
72+
List<CarsClients> carsClientsList = new ArrayList<>();
73+
for ( Client client : clients ) {
74+
for ( Car car : cars ) {
75+
CarsClients carsClients = new CarsClients( "location" );
76+
carsClientsList.add( carsClients );
77+
car.addClient( carsClients );
78+
client.addCar( carsClients );
79+
}
80+
}
81+
return session
82+
.persistAll( carsClientsList.toArray() )
83+
.chain( session::flush );
84+
} );
85+
} )
86+
);
87+
}
88+
89+
@Entity
90+
@Table(name = "Car")
91+
public static class Car {
92+
93+
@Id
94+
@SequenceGenerator(name = "seq_car", sequenceName = "id_seq_car", allocationSize = 1)
95+
@GeneratedValue(generator = "seq_car", strategy = GenerationType.SEQUENCE)
96+
private Long id;
97+
98+
public String brand;
99+
100+
101+
private String model;
102+
103+
@OneToMany(mappedBy = "car")
104+
private Set<CarsClients> clients = new HashSet<>();
105+
106+
public Car() {
107+
}
108+
109+
public Long getId() {
110+
return id;
111+
}
112+
113+
public void setId(Long id) {
114+
this.id = id;
115+
}
116+
117+
public String getBrand() {
118+
return brand;
119+
}
120+
121+
public void setBrand(String brand) {
122+
this.brand = brand;
123+
}
124+
125+
public String getModel() {
126+
return model;
127+
}
128+
129+
public void setModel(String model) {
130+
this.model = model;
131+
}
132+
133+
public Set<CarsClients> getClients() {
134+
return clients;
135+
}
136+
137+
public void setClients(Set<CarsClients> clients) {
138+
this.clients = clients;
139+
}
140+
141+
public void addClient(CarsClients carsClients) {
142+
carsClients.setCar( this );
143+
clients.add( carsClients );
144+
}
145+
}
146+
147+
@Entity
148+
@Table(name = "`Client`")
149+
@Inheritance(strategy = InheritanceType.JOINED)
150+
public static class Client {
151+
152+
@Id
153+
@SequenceGenerator(name = "seq", sequenceName = "id_seq", allocationSize = 1)
154+
@GeneratedValue(generator = "seq", strategy = GenerationType.SEQUENCE)
155+
private Long id;
156+
157+
private String name;
158+
159+
private String email;
160+
161+
private String phone;
162+
163+
@OneToMany(mappedBy = "client")
164+
private Set<CarsClients> cars = new HashSet<>();
165+
166+
public Long getId() {
167+
return id;
168+
}
169+
170+
public void setId(Long id) {
171+
this.id = id;
172+
}
173+
174+
public String getName() {
175+
return name;
176+
}
177+
178+
public void setName(String name) {
179+
this.name = name;
180+
}
181+
182+
public String getEmail() {
183+
return email;
184+
}
185+
186+
public void setEmail(String email) {
187+
this.email = email;
188+
}
189+
190+
public String getPhone() {
191+
return phone;
192+
}
193+
194+
public void setPhone(String phone) {
195+
this.phone = phone;
196+
}
197+
198+
public Set<CarsClients> getCars() {
199+
return cars;
200+
}
201+
202+
public void setCars(Set<CarsClients> cars) {
203+
this.cars = cars;
204+
}
205+
206+
public void addCar(CarsClients carsClients) {
207+
carsClients.setClient( this );
208+
cars.add( carsClients );
209+
}
210+
}
211+
212+
@Entity
213+
@Table(name = "`ClientA`")
214+
public static class ClientA extends Client {
215+
216+
public ClientA() {
217+
}
218+
}
219+
220+
@Entity
221+
@IdClass(CarsClientsId.class)
222+
@Table(name = "cars_clients")
223+
public static class CarsClients {
224+
225+
@Id
226+
@ManyToOne
227+
private Car car;
228+
229+
@Id
230+
@ManyToOne
231+
private Client client;
232+
233+
private String location;
234+
235+
public CarsClients() {
236+
}
237+
238+
public CarsClients(String location) {
239+
this.location = location;
240+
}
241+
242+
public Car getCar() {
243+
return car;
244+
}
245+
246+
public void setCar(Car car) {
247+
this.car = car;
248+
}
249+
250+
public Client getClient() {
251+
return client;
252+
}
253+
254+
public void setClient(Client client) {
255+
this.client = client;
256+
}
257+
258+
public String getLocation() {
259+
return location;
260+
}
261+
262+
public void setLocation(String location) {
263+
this.location = location;
264+
}
265+
}
266+
267+
public static class CarsClientsId {
268+
private Car car;
269+
270+
private Client client;
271+
272+
public CarsClientsId() {
273+
}
274+
275+
public Car getCar() {
276+
return car;
277+
}
278+
279+
public void setCar(Car car) {
280+
this.car = car;
281+
}
282+
283+
public Client getClient() {
284+
return client;
285+
}
286+
287+
public void setClient(Client client) {
288+
this.client = client;
289+
}
290+
}
291+
}

0 commit comments

Comments
 (0)