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,79 @@ public int hashCode() {
204239 return Objects .hash ( name , description , type );
205240 }
206241 }
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+ }
207317}
0 commit comments