Skip to content

Commit c45355d

Browse files
committed
[hibernate#1984] Test for BigDecimal with queries
1 parent 3336e33 commit c45355d

File tree

1 file changed

+105
-0
lines changed
  • hibernate-reactive-core/src/test/java/org/hibernate/reactive

1 file changed

+105
-0
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java

+105
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66
package org.hibernate.reactive;
77

8+
import java.math.BigDecimal;
89
import java.sql.Timestamp;
910
import java.time.LocalDateTime;
1011
import java.time.OffsetDateTime;
1112
import java.util.ArrayList;
1213
import java.util.Collection;
1314
import java.util.List;
15+
import java.util.Objects;
1416

1517
import org.junit.jupiter.api.Assertions;
1618
import org.junit.jupiter.api.Test;
@@ -60,6 +62,60 @@ protected Collection<Class<?>> annotatedEntities() {
6062
return List.of( Book.class, Author.class );
6163
}
6264

65+
private final static BigDecimal PIE = BigDecimal.valueOf( 3.1416 );
66+
private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 );
67+
68+
@Test
69+
public void testBigDecimalAsParameter(VertxTestContext context) {
70+
Author author1 = new Author( "Iain M. Banks" );
71+
Author author2 = new Author( "Neal Stephenson" );
72+
Book book1 = new Book( "1-85723-235-6", "Feersum Endjinn", author1 );
73+
book1.quantity = BigDecimal.valueOf( 11.2 );
74+
Book book2 = new Book( "0-380-97346-4", "Cryptonomicon", author2 );
75+
book2.quantity = PIE;
76+
Book book3 = new Book( "0-553-08853-X", "Snow Crash", author2 );
77+
book3.quantity = TAO;
78+
79+
author1.books.add( book1 );
80+
author2.books.add( book2 );
81+
author2.books.add( book3 );
82+
83+
test( context, getMutinySessionFactory()
84+
.withTransaction( s -> s.persistAll( author1, author2 ) )
85+
// HQL with named parameters
86+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
87+
.createSelectionQuery( "from Book where quantity > :quantity", Book.class )
88+
.setParameter( "quantity", PIE )
89+
.getResultList()
90+
.invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) )
91+
) )
92+
// HQL with positional parameters
93+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
94+
.createSelectionQuery( "from Book where quantity > ?1", Book.class )
95+
.setParameter( 1, PIE )
96+
.getResultList()
97+
.invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) )
98+
) )
99+
// Criteria
100+
.call( () -> {
101+
CriteriaBuilder builder = getSessionFactory().getCriteriaBuilder();
102+
CriteriaQuery<Book> query = builder.createQuery( Book.class );
103+
Root<Book> b = query.from( Book.class );
104+
b.fetch( "author" );
105+
query.where( builder.between(
106+
b.get( "quantity" ),
107+
BigDecimal.valueOf( 4.0 ),
108+
BigDecimal.valueOf( 100.0 )
109+
) );
110+
return getMutinySessionFactory().withTransaction( s -> s
111+
.createQuery( query )
112+
.getResultList()
113+
.invoke( result -> assertThat( result ).containsExactlyInAnyOrder( book1, book3 ) )
114+
);
115+
} )
116+
);
117+
}
118+
63119
@Test
64120
public void testCriteriaEntityQuery(VertxTestContext context) {
65121
Author author1 = new Author( "Iain M. Banks" );
@@ -711,6 +767,28 @@ static class Author {
711767

712768
Author() {
713769
}
770+
771+
@Override
772+
public String toString() {
773+
return id + ":" + name;
774+
}
775+
776+
@Override
777+
public boolean equals(Object o) {
778+
if ( this == o ) {
779+
return true;
780+
}
781+
if ( o == null || getClass() != o.getClass() ) {
782+
return false;
783+
}
784+
Author author = (Author) o;
785+
return Objects.equals( name, author.name );
786+
}
787+
788+
@Override
789+
public int hashCode() {
790+
return Objects.hashCode( name );
791+
}
714792
}
715793

716794
@Entity(name = "Book")
@@ -729,6 +807,8 @@ static class Book {
729807
@ManyToOne(fetch = LAZY)
730808
Author author;
731809

810+
BigDecimal quantity;
811+
732812
Book(String isbn, String title, Author author) {
733813
this.title = title;
734814
this.isbn = isbn;
@@ -737,6 +817,31 @@ static class Book {
737817

738818
Book() {
739819
}
820+
821+
@Override
822+
public String toString() {
823+
return id + ":" + title + ":" + isbn + ":" + quantity;
824+
}
825+
826+
@Override
827+
public boolean equals(Object o) {
828+
if ( this == o ) {
829+
return true;
830+
}
831+
if ( o == null || getClass() != o.getClass() ) {
832+
return false;
833+
}
834+
Book book = (Book) o;
835+
return Objects.equals( isbn, book.isbn ) && Objects.equals(
836+
title,
837+
book.title
838+
);
839+
}
840+
841+
@Override
842+
public int hashCode() {
843+
return Objects.hash( isbn, title );
844+
}
740845
}
741846

742847
}

0 commit comments

Comments
 (0)