From efb7cbba89e6d4b005462320457e437083e31475 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 12 Sep 2024 12:28:14 +0200 Subject: [PATCH] [#1984] Test for BigDecimal with queries --- .../org/hibernate/reactive/QueryTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java index 00e42e82e..d6146b2e5 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java @@ -5,12 +5,14 @@ */ package org.hibernate.reactive; +import java.math.BigDecimal; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -60,6 +62,60 @@ protected Collection> annotatedEntities() { return List.of( Book.class, Author.class ); } + private final static BigDecimal PIE = BigDecimal.valueOf( 3.1416 ); + private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 ); + + @Test + public void testBigDecimalAsParameter(VertxTestContext context) { + Author author1 = new Author( "Iain M. Banks" ); + Author author2 = new Author( "Neal Stephenson" ); + Book book1 = new Book( "1-85723-235-6", "Feersum Endjinn", author1 ); + book1.quantity = BigDecimal.valueOf( 11.2 ); + Book book2 = new Book( "0-380-97346-4", "Cryptonomicon", author2 ); + book2.quantity = PIE; + Book book3 = new Book( "0-553-08853-X", "Snow Crash", author2 ); + book3.quantity = TAO; + + author1.books.add( book1 ); + author2.books.add( book2 ); + author2.books.add( book3 ); + + test( context, getMutinySessionFactory() + .withTransaction( s -> s.persistAll( author1, author2 ) ) + // HQL with named parameters + .chain( () -> getMutinySessionFactory().withTransaction( s -> s + .createSelectionQuery( "from Book where quantity > :quantity", Book.class ) + .setParameter( "quantity", PIE ) + .getResultList() + .invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) ) + ) ) + // HQL with positional parameters + .chain( () -> getMutinySessionFactory().withTransaction( s -> s + .createSelectionQuery( "from Book where quantity > ?1", Book.class ) + .setParameter( 1, PIE ) + .getResultList() + .invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) ) + ) ) + // Criteria + .call( () -> { + CriteriaBuilder builder = getSessionFactory().getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery( Book.class ); + Root b = query.from( Book.class ); + b.fetch( "author" ); + query.where( builder.between( + b.get( "quantity" ), + BigDecimal.valueOf( 4.0 ), + BigDecimal.valueOf( 100.0 ) + ) ); + return getMutinySessionFactory().withTransaction( s -> s + .createQuery( query ) + .getResultList() + .invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) ) + ); + } ) + ); + } + @Test public void testCriteriaEntityQuery(VertxTestContext context) { Author author1 = new Author( "Iain M. Banks" ); @@ -711,6 +767,28 @@ static class Author { Author() { } + + @Override + public String toString() { + return id + ":" + name; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Author author = (Author) o; + return Objects.equals( name, author.name ); + } + + @Override + public int hashCode() { + return Objects.hashCode( name ); + } } @Entity(name = "Book") @@ -729,6 +807,8 @@ static class Book { @ManyToOne(fetch = LAZY) Author author; + BigDecimal quantity; + Book(String isbn, String title, Author author) { this.title = title; this.isbn = isbn; @@ -737,6 +817,31 @@ static class Book { Book() { } + + @Override + public String toString() { + return id + ":" + title + ":" + isbn + ":" + quantity; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Book book = (Book) o; + return Objects.equals( isbn, book.isbn ) && Objects.equals( + title, + book.title + ); + } + + @Override + public int hashCode() { + return Objects.hash( isbn, title ); + } } }