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,25 @@ 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 (
57+ context , getMutinySessionFactory ()
58+ .withTransaction ( (session , transaction ) -> session
59+ .persistAll ( spelt , rye , almond , miller , camilleri , circe , shapeOfWater , spider )
60+ )
61+ );
4662 }
4763
4864 @ Test
@@ -129,6 +145,21 @@ public void testFromQuery(VertxTestContext context) {
129145 );
130146 }
131147
148+ @ Test
149+ public void testSelectNewConstructor (VertxTestContext context ) {
150+ test ( context , getMutinySessionFactory ()
151+ .withTransaction ( session -> session
152+ .createQuery ( "SELECT NEW Book(b.title, b.author) FROM Book b ORDER BY b.title DESC" , Book .class )
153+ .getResultList ()
154+ )
155+ .invoke ( books -> assertThat ( books ).containsExactly (
156+ new Book ( shapeOfWater .title , camilleri ),
157+ new Book ( spider .title , camilleri ),
158+ new Book ( circe .title , miller )
159+ ) )
160+ );
161+ }
162+
132163 @ Entity (name = "Flour" )
133164 @ Table (name = "Flour" )
134165 public static class Flour {
@@ -204,4 +235,122 @@ public int hashCode() {
204235 return Objects .hash ( name , description , type );
205236 }
206237 }
238+
239+ @ Entity (name = "Book" )
240+ @ Table (name = "Book_HQL" )
241+ public static class Book {
242+ @ Id
243+ @ GeneratedValue
244+ private Integer id ;
245+
246+ private String isbn ;
247+
248+ private String title ;
249+
250+ @ ManyToOne (fetch = LAZY )
251+ private Author author ;
252+
253+ public Book () {
254+ }
255+
256+ public Book (String title , Author author ) {
257+ this .title = title ;
258+ this .author = author ;
259+ }
260+
261+ public Book (String isbn , String title , Author author ) {
262+ this .isbn = isbn ;
263+ this .title = title ;
264+ this .author = author ;
265+ author .books .add ( this );
266+ }
267+
268+ public Integer getId () {
269+ return id ;
270+ }
271+
272+ public String getIsbn () {
273+ return isbn ;
274+ }
275+
276+ public String getTitle () {
277+ return title ;
278+ }
279+
280+ public Author getAuthor () {
281+ return author ;
282+ }
283+
284+ @ Override
285+ public boolean equals (Object o ) {
286+ if ( o == null || getClass () != o .getClass () ) {
287+ return false ;
288+ }
289+ Book book = (Book ) o ;
290+ return Objects .equals ( isbn , book .isbn ) && Objects .equals (
291+ title ,
292+ book .title
293+ ) && Objects .equals ( author , book .author );
294+ }
295+
296+ @ Override
297+ public int hashCode () {
298+ return Objects .hash ( isbn , title , author );
299+ }
300+
301+ @ Override
302+ public String toString () {
303+ return id + ":" + isbn + ":" + title + ":" + author ;
304+ }
305+ }
306+
307+ @ Entity (name = "Author" )
308+ @ Table (name = "Author_HQL" )
309+ public static class Author {
310+ @ Id @ GeneratedValue
311+ private Integer id ;
312+
313+ private String name ;
314+
315+ @ OneToMany (mappedBy = "author" , cascade = PERSIST )
316+ private List <Book > books = new ArrayList <>();
317+
318+ public Author () {
319+ }
320+
321+ public Author (String name ) {
322+ this .name = name ;
323+ }
324+
325+ public Integer getId () {
326+ return id ;
327+ }
328+
329+ public String getName () {
330+ return name ;
331+ }
332+
333+ public List <Book > getBooks () {
334+ return books ;
335+ }
336+
337+ @ Override
338+ public boolean equals (Object o ) {
339+ if ( o == null || getClass () != o .getClass () ) {
340+ return false ;
341+ }
342+ Author author = (Author ) o ;
343+ return Objects .equals ( name , author .name );
344+ }
345+
346+ @ Override
347+ public int hashCode () {
348+ return Objects .hashCode ( name );
349+ }
350+
351+ @ Override
352+ public String toString () {
353+ return id + ":" + name ;
354+ }
355+ }
207356}
0 commit comments