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,81 @@ public int hashCode() {
204
239
return Objects .hash ( name , description , type );
205
240
}
206
241
}
242
+
243
+ @ Entity (name = "Book" )
244
+ @ Table (name = "Book_HQL" )
245
+ public static class Book {
246
+ @ Id
247
+ @ GeneratedValue
248
+ private Integer id ;
249
+
250
+ private String isbn ;
251
+
252
+ private String title ;
253
+
254
+ @ ManyToOne (fetch = LAZY )
255
+ private Author author ;
256
+
257
+ public Book () {
258
+ }
259
+
260
+ public Book (String title , Author author ) {
261
+ this .title = title ;
262
+ this .author = author ;
263
+ }
264
+
265
+ public Book (String isbn , String title , Author author ) {
266
+ this .isbn = isbn ;
267
+ this .title = title ;
268
+ this .author = author ;
269
+ author .books .add ( this );
270
+ }
271
+
272
+ public Integer getId () {
273
+ return id ;
274
+ }
275
+
276
+ public String getIsbn () {
277
+ return isbn ;
278
+ }
279
+
280
+ public String getTitle () {
281
+ return title ;
282
+ }
283
+
284
+ public Author getAuthor () {
285
+ return author ;
286
+ }
287
+ }
288
+
289
+ @ Entity (name = "Author" )
290
+ @ Table (name = "Author_HQL" )
291
+ public static class Author {
292
+ @ Id @ GeneratedValue
293
+ private Integer id ;
294
+
295
+ private String name ;
296
+
297
+ @ OneToMany (mappedBy = "author" , cascade = PERSIST )
298
+ private List <Book > books = new ArrayList <>();
299
+
300
+ public Author () {
301
+ }
302
+
303
+ public Author (String name ) {
304
+ this .name = name ;
305
+ }
306
+
307
+ public Integer getId () {
308
+ return id ;
309
+ }
310
+
311
+ public String getName () {
312
+ return name ;
313
+ }
314
+
315
+ public List <Book > getBooks () {
316
+ return books ;
317
+ }
318
+ }
207
319
}
0 commit comments