11
11
12
12
import org .hibernate .Session ;
13
13
import org .hibernate .SessionFactory ;
14
+ import org .hibernate .annotations .JdbcType ;
15
+ import org .hibernate .annotations .JdbcTypeCode ;
14
16
import org .hibernate .boot .registry .StandardServiceRegistry ;
15
17
import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
16
18
import org .hibernate .cfg .Configuration ;
19
+ import org .hibernate .dialect .PostgreSQLJsonPGObjectJsonbType ;
17
20
import org .hibernate .reactive .annotations .DisabledFor ;
21
+ import org .hibernate .type .SqlTypes ;
18
22
19
23
import org .junit .jupiter .api .AfterEach ;
20
24
import org .junit .jupiter .api .BeforeEach ;
21
25
import org .junit .jupiter .api .Test ;
22
26
23
27
import io .vertx .junit5 .Timeout ;
24
28
import io .vertx .junit5 .VertxTestContext ;
29
+ import jakarta .persistence .Embeddable ;
25
30
import jakarta .persistence .Entity ;
26
31
import jakarta .persistence .Id ;
27
32
import jakarta .persistence .Table ;
28
33
29
34
import static java .util .concurrent .TimeUnit .MINUTES ;
35
+ import static org .assertj .core .api .Assertions .assertThat ;
30
36
import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .COCKROACHDB ;
31
37
import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .DB2 ;
32
38
import static org .hibernate .reactive .containers .DatabaseConfiguration .dbType ;
33
39
import static org .hibernate .reactive .provider .Settings .DIALECT ;
34
40
import static org .hibernate .reactive .provider .Settings .DRIVER ;
35
- import static org .junit .jupiter .api .Assertions .assertEquals ;
36
41
37
42
@ Timeout (value = 10 , timeUnit = MINUTES )
38
43
@@ -48,9 +53,12 @@ public class ORMReactivePersistenceTest extends BaseReactiveTest {
48
53
49
54
@ Override
50
55
protected Collection <Class <?>> annotatedEntities () {
51
- return List .of ( Flour .class );
56
+ return List .of ( Book .class );
52
57
}
53
58
59
+ final Book fakeHistory = new Book ( 3 , "Fake History" , new Book .Author ( "Jo" , "Hedwig Teeuwisse" ) );
60
+ final Book theBookOfM = new Book ( 5 , "The Book of M" , new Book .Author ( "Peng" , "Shepherd" ) );
61
+
54
62
@ BeforeEach
55
63
public void prepareOrmFactory () {
56
64
Configuration configuration = constructConfiguration ();
@@ -69,94 +77,97 @@ public void closeOrmFactory() {
69
77
ormFactory .close ();
70
78
}
71
79
72
- @ Test
73
- public void testORMWithStageSession (VertxTestContext context ) {
74
- final Flour almond = new Flour ( 1 , "Almond" , "made from ground almonds." , "Gluten free" );
75
-
76
- try (Session session = ormFactory .openSession ()) {
77
- session .beginTransaction ();
78
- session .persist ( almond );
79
- session .getTransaction ().commit ();
80
- }
81
-
82
- // Check database with Stage session and verify 'almond' flour exists
83
- test ( context , openSession ()
84
- .thenCompose ( stageSession -> stageSession .find ( Flour .class , almond .id ) )
85
- .thenAccept ( entityFound -> assertEquals ( almond , entityFound ) )
86
- );
87
- }
88
-
89
80
@ Test
90
81
public void testORMWitMutinySession (VertxTestContext context ) {
91
- final Flour rose = new Flour ( 2 , "Rose" , "made from ground rose pedals." , "Full fragrance" );
82
+ try (Session ormSession = ormFactory .openSession ()) {
83
+ ormSession .beginTransaction ();
84
+ ormSession .persist ( theBookOfM );
85
+ ormSession .persist ( fakeHistory );
86
+ ormSession .getTransaction ().commit ();
87
+ }
92
88
93
89
try (Session ormSession = ormFactory .openSession ()) {
94
90
ormSession .beginTransaction ();
95
- ormSession .persist ( rose );
91
+ Book result = ormSession .createNativeQuery ( "select * from BookWithJson where id = 3" , Book .class )
92
+ .getSingleResult ();
93
+ assertThat ( result ).isEqualTo ( theBookOfM );
96
94
ormSession .getTransaction ().commit ();
97
95
}
98
96
99
- // Check database with Mutiny session and verify 'rose' flour exists
100
- test ( context , openMutinySession ()
101
- .chain ( session -> session .find ( Flour .class , rose .id ) )
102
- .invoke ( foundRose -> assertEquals ( rose , foundRose ) )
103
- );
104
97
}
105
98
106
- @ Entity (name = "Flour" )
107
- @ Table (name = "Flour" )
108
- public static class Flour {
99
+ @ Entity (name = "Book" )
100
+ @ Table (name = "BookWithJson" )
101
+ public static class Book {
102
+
109
103
@ Id
110
- private Integer id ;
111
- private String name ;
112
- private String description ;
113
- private String type ;
104
+ Integer id ;
114
105
115
- public Flour () {
116
- }
106
+ String title ;
117
107
118
- public Flour (Integer id , String name , String description , String type ) {
119
- this .id = id ;
120
- this .name = name ;
121
- this .description = description ;
122
- this .type = type ;
123
- }
108
+ @ JdbcTypeCode (SqlTypes .JSON )
109
+ @ JdbcType (PostgreSQLJsonPGObjectJsonbType .class )
110
+ Author author ;
124
111
125
- public Integer getId () {
126
- return id ;
127
- }
112
+ @ Embeddable
113
+ public static class Author {
114
+ private String name ;
115
+ private String surname ;
128
116
129
- public void setId (Integer id ) {
130
- this .id = id ;
131
- }
117
+ public Author () {
118
+ }
132
119
133
- public String getName () {
134
- return name ;
135
- }
120
+ public Author (String name , String surname ) {
121
+ this .name = name ;
122
+ this .surname = surname ;
123
+ }
136
124
137
- public void setName ( String name ) {
138
- this . name = name ;
139
- }
125
+ public String getName ( ) {
126
+ return name ;
127
+ }
140
128
141
- public String getDescription ( ) {
142
- return description ;
143
- }
129
+ public void setName ( String name ) {
130
+ this . name = name ;
131
+ }
144
132
145
- public void setDescription (String description ) {
146
- this .description = description ;
147
- }
133
+ public String getSurname () {
134
+ return surname ;
135
+ }
136
+
137
+ public void setSurname (String surname ) {
138
+ this .surname = surname ;
139
+ }
140
+
141
+ @ Override
142
+ public boolean equals (Object o ) {
143
+ if ( this == o ) {
144
+ return true ;
145
+ }
146
+ if ( o == null || getClass () != o .getClass () ) {
147
+ return false ;
148
+ }
149
+ Book .Author author = (Book .Author ) o ;
150
+ return Objects .equals ( name , author .name ) && Objects .equals ( surname , author .surname );
151
+ }
148
152
149
- public String getType () {
150
- return type ;
153
+ @ Override
154
+ public int hashCode () {
155
+ return Objects .hash ( name , surname );
156
+ }
157
+
158
+ @ Override
159
+ public String toString () {
160
+ return name + ' ' + surname ;
161
+ }
151
162
}
152
163
153
- public void setType (String type ) {
154
- this .type = type ;
164
+ public Book () {
155
165
}
156
166
157
- @ Override
158
- public String toString () {
159
- return name ;
167
+ public Book (Integer id , String title , Author author ) {
168
+ this .id = id ;
169
+ this .title = title ;
170
+ this .author = author ;
160
171
}
161
172
162
173
@ Override
@@ -167,15 +178,21 @@ public boolean equals(Object o) {
167
178
if ( o == null || getClass () != o .getClass () ) {
168
179
return false ;
169
180
}
170
- Flour flour = (Flour ) o ;
171
- return Objects .equals ( name , flour .name ) &&
172
- Objects .equals ( description , flour .description ) &&
173
- Objects .equals ( type , flour .type );
181
+ Book book = (Book ) o ;
182
+ return Objects .equals ( id , book .id ) && Objects .equals (
183
+ title ,
184
+ book .title
185
+ ) && Objects .equals ( author , book .author );
174
186
}
175
187
176
188
@ Override
177
189
public int hashCode () {
178
- return Objects .hash ( name , description , type );
190
+ return Objects .hash ( id , title , author );
191
+ }
192
+
193
+ @ Override
194
+ public String toString () {
195
+ return id + ":" + title + ":" + author ;
179
196
}
180
197
}
181
198
}
0 commit comments