5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
+ import java .math .BigDecimal ;
8
9
import java .sql .Timestamp ;
9
10
import java .time .LocalDateTime ;
10
11
import java .time .OffsetDateTime ;
11
12
import java .util .ArrayList ;
12
13
import java .util .Collection ;
13
14
import java .util .List ;
15
+ import java .util .Objects ;
14
16
15
17
import org .junit .jupiter .api .Assertions ;
16
18
import org .junit .jupiter .api .Test ;
@@ -60,6 +62,60 @@ protected Collection<Class<?>> annotatedEntities() {
60
62
return List .of ( Book .class , Author .class );
61
63
}
62
64
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
+
63
119
@ Test
64
120
public void testCriteriaEntityQuery (VertxTestContext context ) {
65
121
Author author1 = new Author ( "Iain M. Banks" );
@@ -711,6 +767,28 @@ static class Author {
711
767
712
768
Author () {
713
769
}
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
+ }
714
792
}
715
793
716
794
@ Entity (name = "Book" )
@@ -729,6 +807,8 @@ static class Book {
729
807
@ ManyToOne (fetch = LAZY )
730
808
Author author ;
731
809
810
+ BigDecimal quantity ;
811
+
732
812
Book (String isbn , String title , Author author ) {
733
813
this .title = title ;
734
814
this .isbn = isbn ;
@@ -737,6 +817,31 @@ static class Book {
737
817
738
818
Book () {
739
819
}
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
+ }
740
845
}
741
846
742
847
}
0 commit comments