55 */
66package org .hibernate .reactive ;
77
8+ import jakarta .persistence .GeneratedValue ;
9+ import jakarta .persistence .ManyToOne ;
10+ import jakarta .persistence .OneToMany ;
11+ import java .util .ArrayList ;
812import java .util .Collection ;
913import java .util .List ;
1014import java .util .Objects ;
2024import jakarta .persistence .Id ;
2125import jakarta .persistence .Table ;
2226
27+ import static jakarta .persistence .CascadeType .PERSIST ;
28+ import static jakarta .persistence .FetchType .LAZY ;
2329import static java .util .concurrent .TimeUnit .MINUTES ;
2430import static org .assertj .core .api .Assertions .assertThat ;
2531import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -34,15 +40,22 @@ public class HQLQueryTest extends BaseReactiveTest {
3440 Flour rye = new Flour ( 2 , "Rye" , "Used to bake the traditional sourdough breads of Germany." , "Wheat flour" );
3541 Flour almond = new Flour ( 3 , "Almond" , "made from ground almonds." , "Gluten free" );
3642
43+ Author miller = new Author ( "Madeline Miller" );
44+ Author camilleri = new Author ( "Andrea Camilleri" );
45+ Book circe = new Book ( "9780316556347" , "Circe" , miller );
46+ Book shapeOfWater = new Book ( "0-330-49286-1 " , "The Shape of Water" , camilleri );
47+ Book spider = new Book ( "978-0-14-311203-7" , "The Patience of the Spider" , camilleri );
48+
3749 @ Override
3850 protected Collection <Class <?>> annotatedEntities () {
39- return List .of ( Flour .class );
51+ return List .of ( Flour .class , Book . class , Author . class );
4052 }
4153
4254 @ BeforeEach
4355 public void populateDb (VertxTestContext context ) {
44- test ( context , getMutinySessionFactory ()
45- .withTransaction ( (session , transaction ) -> session .persistAll ( spelt , rye , almond ) ) );
56+ test ( context , getMutinySessionFactory ().withTransaction ( session -> session
57+ .persistAll ( spelt , rye , almond , miller , camilleri , circe , shapeOfWater , spider ) )
58+ );
4659 }
4760
4861 @ Test
@@ -129,6 +142,21 @@ public void testFromQuery(VertxTestContext context) {
129142 );
130143 }
131144
145+ @ Test
146+ public void testSelectNewConstructor (VertxTestContext context ) {
147+ test ( context , getMutinySessionFactory ()
148+ .withTransaction ( session -> session
149+ .createQuery ( "SELECT NEW Book(b.title, b.author) FROM Book b ORDER BY b.title DESC" , Book .class )
150+ .getResultList ()
151+ )
152+ .invoke ( books -> assertThat ( books ).containsExactly (
153+ new Book ( shapeOfWater .title , camilleri ),
154+ new Book ( spider .title , camilleri ),
155+ new Book ( circe .title , miller )
156+ ) )
157+ );
158+ }
159+
132160 @ Entity (name = "Flour" )
133161 @ Table (name = "Flour" )
134162 public static class Flour {
@@ -204,4 +232,122 @@ public int hashCode() {
204232 return Objects .hash ( name , description , type );
205233 }
206234 }
235+
236+ @ Entity (name = "Book" )
237+ @ Table (name = "Book_HQL" )
238+ public static class Book {
239+ @ Id
240+ @ GeneratedValue
241+ private Integer id ;
242+
243+ private String isbn ;
244+
245+ private String title ;
246+
247+ @ ManyToOne (fetch = LAZY )
248+ private Author author ;
249+
250+ public Book () {
251+ }
252+
253+ public Book (String title , Author author ) {
254+ this .title = title ;
255+ this .author = author ;
256+ }
257+
258+ public Book (String isbn , String title , Author author ) {
259+ this .isbn = isbn ;
260+ this .title = title ;
261+ this .author = author ;
262+ author .books .add ( this );
263+ }
264+
265+ public Integer getId () {
266+ return id ;
267+ }
268+
269+ public String getIsbn () {
270+ return isbn ;
271+ }
272+
273+ public String getTitle () {
274+ return title ;
275+ }
276+
277+ public Author getAuthor () {
278+ return author ;
279+ }
280+
281+ @ Override
282+ public boolean equals (Object o ) {
283+ if ( o == null || getClass () != o .getClass () ) {
284+ return false ;
285+ }
286+ Book book = (Book ) o ;
287+ return Objects .equals ( isbn , book .isbn ) && Objects .equals (
288+ title ,
289+ book .title
290+ ) && Objects .equals ( author , book .author );
291+ }
292+
293+ @ Override
294+ public int hashCode () {
295+ return Objects .hash ( isbn , title , author );
296+ }
297+
298+ @ Override
299+ public String toString () {
300+ return id + ":" + isbn + ":" + title + ":" + author ;
301+ }
302+ }
303+
304+ @ Entity (name = "Author" )
305+ @ Table (name = "Author_HQL" )
306+ public static class Author {
307+ @ Id @ GeneratedValue
308+ private Integer id ;
309+
310+ private String name ;
311+
312+ @ OneToMany (mappedBy = "author" , cascade = PERSIST )
313+ private List <Book > books = new ArrayList <>();
314+
315+ public Author () {
316+ }
317+
318+ public Author (String name ) {
319+ this .name = name ;
320+ }
321+
322+ public Integer getId () {
323+ return id ;
324+ }
325+
326+ public String getName () {
327+ return name ;
328+ }
329+
330+ public List <Book > getBooks () {
331+ return books ;
332+ }
333+
334+ @ Override
335+ public boolean equals (Object o ) {
336+ if ( o == null || getClass () != o .getClass () ) {
337+ return false ;
338+ }
339+ Author author = (Author ) o ;
340+ return Objects .equals ( name , author .name );
341+ }
342+
343+ @ Override
344+ public int hashCode () {
345+ return Objects .hashCode ( name );
346+ }
347+
348+ @ Override
349+ public String toString () {
350+ return id + ":" + name ;
351+ }
352+ }
207353}
0 commit comments