|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import io.vertx.junit5.VertxTestContext; |
| 12 | +import jakarta.persistence.Entity; |
| 13 | +import jakarta.persistence.GeneratedValue; |
| 14 | +import jakarta.persistence.Id; |
| 15 | +import jakarta.persistence.ManyToOne; |
| 16 | +import jakarta.persistence.OneToMany; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static jakarta.persistence.CascadeType.PERSIST; |
| 22 | +import static jakarta.persistence.FetchType.LAZY; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +public class HqlSelectNewConstructorTest extends BaseReactiveTest { |
| 26 | + |
| 27 | + @Override |
| 28 | + protected Collection<Class<?>> annotatedEntities() { |
| 29 | + return List.of( Book.class, Author.class ); |
| 30 | + } |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void prepareDb(VertxTestContext context) { |
| 34 | + Author author1 = new Author("Iain M. Banks"); |
| 35 | + Author author2 = new Author("Neal Stephenson"); |
| 36 | + Book book1 = new Book("1-85723-235-6", "Feersum Endjinn", author1); |
| 37 | + Book book2 = new Book("0-380-97346-4", "Cryptonomicon", author2); |
| 38 | + Book book3 = new Book("0-553-08853-X", "Snow Crash", author2); |
| 39 | + test( context, getMutinySessionFactory().withTransaction( session -> session.persistAll( |
| 40 | + author1, author2, book1, book2, book3 |
| 41 | + ) ) ); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testSimpleUpdateSameTransaction(VertxTestContext context) { |
| 46 | + test( context, getMutinySessionFactory() |
| 47 | + .withTransaction( session -> |
| 48 | + session.createQuery( |
| 49 | + "SELECT NEW Book(book.author) from Book book order by title desc", |
| 50 | + Book.class |
| 51 | + ) |
| 52 | + .getResultList() |
| 53 | + ) |
| 54 | + .invoke( books -> assertThat( books ).hasSize( 3 ) ) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Entity(name = "Book") |
| 59 | + public static class Book { |
| 60 | + @Id |
| 61 | + @GeneratedValue |
| 62 | + private Integer id; |
| 63 | + |
| 64 | + private String isbn; |
| 65 | + |
| 66 | + private String title; |
| 67 | + |
| 68 | + @ManyToOne(fetch = LAZY) |
| 69 | + private Author author; |
| 70 | + |
| 71 | + public Book() { |
| 72 | + } |
| 73 | + |
| 74 | + public Book(Author author) { |
| 75 | + this.author = author; |
| 76 | + } |
| 77 | + |
| 78 | + public Book(String isbn, String title, Author author) { |
| 79 | + this.isbn = isbn; |
| 80 | + this.title = title; |
| 81 | + this.author = author; |
| 82 | + author.books.add( this ); |
| 83 | + } |
| 84 | + |
| 85 | + public Integer getId() { |
| 86 | + return id; |
| 87 | + } |
| 88 | + |
| 89 | + public String getIsbn() { |
| 90 | + return isbn; |
| 91 | + } |
| 92 | + |
| 93 | + public Author getAuthor() { |
| 94 | + return author; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Entity(name = "Author") |
| 99 | + public static class Author { |
| 100 | + @Id @GeneratedValue |
| 101 | + private Integer id; |
| 102 | + |
| 103 | + private String name; |
| 104 | + |
| 105 | + @OneToMany(mappedBy = "author", cascade = PERSIST) |
| 106 | + private List<Book> books = new ArrayList<>(); |
| 107 | + |
| 108 | + public Author() { |
| 109 | + } |
| 110 | + |
| 111 | + public Author(String name) { |
| 112 | + this.name = name; |
| 113 | + } |
| 114 | + |
| 115 | + public Integer getId() { |
| 116 | + return id; |
| 117 | + } |
| 118 | + |
| 119 | + public String getName() { |
| 120 | + return name; |
| 121 | + } |
| 122 | + |
| 123 | + public List<Book> getBooks() { |
| 124 | + return books; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments