5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
+ import jakarta .persistence .GeneratedValue ;
9
+ import jakarta .persistence .ManyToOne ;
10
+ import jakarta .persistence .OneToMany ;
11
+ import java .util .ArrayList ;
8
12
import java .util .Collection ;
9
13
import java .util .List ;
10
14
import java .util .Objects ;
20
24
import jakarta .persistence .Id ;
21
25
import jakarta .persistence .Table ;
22
26
27
+ import static jakarta .persistence .CascadeType .PERSIST ;
28
+ import static jakarta .persistence .FetchType .LAZY ;
23
29
import static java .util .concurrent .TimeUnit .MINUTES ;
24
30
import static org .assertj .core .api .Assertions .assertThat ;
25
31
import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -34,15 +40,21 @@ public class HQLQueryTest extends BaseReactiveTest {
34
40
Flour rye = new Flour ( 2 , "Rye" , "Used to bake the traditional sourdough breads of Germany." , "Wheat flour" );
35
41
Flour almond = new Flour ( 3 , "Almond" , "made from ground almonds." , "Gluten free" );
36
42
43
+ Author author1 = new Author ( "Madeline Miller" );
44
+ Author author2 = new Author ( "Andrea Camilleri" );
45
+ Book book1 = new Book ( "9780316556347" , "Circe" , author1 );
46
+ Book book2 = new Book ( "0-330-49286-1 " , "The Shape of Water" , author2 );
47
+ Book book3 = new Book ( "978-0-14-311203-7" , "The Patience of the Spider" , author2 );
48
+
37
49
@ Override
38
50
protected Collection <Class <?>> annotatedEntities () {
39
- return List .of ( Flour .class );
51
+ return List .of ( Flour .class , Book . class , Author . class );
40
52
}
41
53
42
54
@ BeforeEach
43
55
public void populateDb (VertxTestContext context ) {
44
56
test ( context , getMutinySessionFactory ()
45
- .withTransaction ( (session , transaction ) -> session .persistAll ( spelt , rye , almond ) ) );
57
+ .withTransaction ( (session , transaction ) -> session .persistAll ( spelt , rye , almond , author1 , author2 , book1 , book2 , book3 ) ) );
46
58
}
47
59
48
60
@ Test
@@ -129,6 +141,29 @@ public void testFromQuery(VertxTestContext context) {
129
141
);
130
142
}
131
143
144
+ @ Test
145
+ public void testSelectNewConstructor (VertxTestContext context ) {
146
+ test ( context , getMutinySessionFactory ()
147
+ .withTransaction ( session -> session .createQuery (
148
+ "SELECT NEW Book(b.title, b.author) FROM Book b ORDER BY b.title DESC" ,
149
+ Book .class
150
+ ).getResultList ()
151
+ )
152
+ .invoke ( books -> {
153
+ assertThat ( books ).hasSize ( 3 );
154
+ books .forEach ( book -> {
155
+ String name = book .getAuthor ().getName ();
156
+ if ( book .getTitle ().equals ( "Circe" ) ) {
157
+ assertThat ( name ).isEqualTo ( "Madeline Miller" );
158
+ }
159
+ else {
160
+ assertThat ( name ).isEqualTo ( "Andrea Camilleri" );
161
+ }
162
+ } );
163
+ } )
164
+ );
165
+ }
166
+
132
167
@ Entity (name = "Flour" )
133
168
@ Table (name = "Flour" )
134
169
public static class Flour {
@@ -204,4 +239,79 @@ public int hashCode() {
204
239
return Objects .hash ( name , description , type );
205
240
}
206
241
}
242
+
243
+ @ Entity (name = "Book" )
244
+ public static class Book {
245
+ @ Id
246
+ @ GeneratedValue
247
+ private Integer id ;
248
+
249
+ private String isbn ;
250
+
251
+ private String title ;
252
+
253
+ @ ManyToOne (fetch = LAZY )
254
+ private Author author ;
255
+
256
+ public Book () {
257
+ }
258
+
259
+ public Book (String title , Author author ) {
260
+ this .title = title ;
261
+ this .author = author ;
262
+ }
263
+
264
+ public Book (String isbn , String title , Author author ) {
265
+ this .isbn = isbn ;
266
+ this .title = title ;
267
+ this .author = author ;
268
+ author .books .add ( this );
269
+ }
270
+
271
+ public Integer getId () {
272
+ return id ;
273
+ }
274
+
275
+ public String getIsbn () {
276
+ return isbn ;
277
+ }
278
+
279
+ public String getTitle () {
280
+ return title ;
281
+ }
282
+
283
+ public Author getAuthor () {
284
+ return author ;
285
+ }
286
+ }
287
+
288
+ @ Entity (name = "Author" )
289
+ public static class Author {
290
+ @ Id @ GeneratedValue
291
+ private Integer id ;
292
+
293
+ private String name ;
294
+
295
+ @ OneToMany (mappedBy = "author" , cascade = PERSIST )
296
+ private List <Book > books = new ArrayList <>();
297
+
298
+ public Author () {
299
+ }
300
+
301
+ public Author (String name ) {
302
+ this .name = name ;
303
+ }
304
+
305
+ public Integer getId () {
306
+ return id ;
307
+ }
308
+
309
+ public String getName () {
310
+ return name ;
311
+ }
312
+
313
+ public List <Book > getBooks () {
314
+ return books ;
315
+ }
316
+ }
207
317
}
0 commit comments