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,21 @@ 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 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+
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 ) {
4456 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 ) ) );
4658 }
4759
4860 @ Test
@@ -129,6 +141,29 @@ public void testFromQuery(VertxTestContext context) {
129141 );
130142 }
131143
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+
132167 @ Entity (name = "Flour" )
133168 @ Table (name = "Flour" )
134169 public static class Flour {
@@ -204,4 +239,81 @@ public int hashCode() {
204239 return Objects .hash ( name , description , type );
205240 }
206241 }
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+ }
207319}
0 commit comments