26
26
import jakarta .persistence .criteria .Root ;
27
27
28
28
import static java .util .concurrent .TimeUnit .MINUTES ;
29
+ import static org .assertj .core .api .Assertions .assertThat ;
29
30
import static org .junit .jupiter .api .Assertions .assertEquals ;
30
31
import static org .junit .jupiter .api .Assertions .assertFalse ;
31
32
import static org .junit .jupiter .api .Assertions .assertNotNull ;
@@ -37,7 +38,7 @@ public class ReactiveStatelessSessionTest extends BaseReactiveTest {
37
38
38
39
@ Override
39
40
protected Collection <Class <?>> annotatedEntities () {
40
- return List .of ( GuineaPig .class );
41
+ return List .of ( Record . class , GuineaPig .class );
41
42
}
42
43
43
44
@ Test
@@ -206,6 +207,141 @@ private void assertThatPigsAreEqual( GuineaPig expected, GuineaPig actual) {
206
207
assertEquals ( expected .getName (), actual .getName () );
207
208
}
208
209
210
+ /**
211
+ * Mutiny version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
212
+ */
213
+ @ Test
214
+ public void testMutinyUpsertWithEntityName (VertxTestContext context ) {
215
+ test ( context , getMutinySessionFactory ().withStatelessTransaction ( ss -> ss
216
+ .upsert ( Record .class .getName (), new Record ( 123L , "hello earth" ) )
217
+ .call ( () -> ss .upsert ( Record .class .getName (), new Record ( 456L , "hello mars" ) ) )
218
+ )
219
+ .call ( v -> getMutinySessionFactory ().withStatelessTransaction ( ss -> ss
220
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
221
+ .invoke ( results -> assertThat ( results ).containsExactly (
222
+ new Record ( 123L , "hello earth" ),
223
+ new Record ( 456L , "hello mars" )
224
+ ) )
225
+ )
226
+ .call ( () -> getMutinySessionFactory ().withStatelessTransaction ( ss -> ss
227
+ .upsert ( Record .class .getName (), new Record ( 123L , "goodbye earth" ) )
228
+ ) )
229
+ .call ( v -> getMutinySessionFactory ().withStatelessTransaction ( ss -> ss
230
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
231
+ .invoke ( results -> assertThat ( results ).containsExactly (
232
+ new Record ( 123L , "goodbye earth" ),
233
+ new Record ( 456L , "hello mars" )
234
+ ) )
235
+ )
236
+ );
237
+ }
238
+
239
+ /**
240
+ * CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
241
+ */
242
+ @ Test
243
+ public void testStageUpsert (VertxTestContext context ) {
244
+ test ( context , getSessionFactory ().withStatelessTransaction ( ss -> ss
245
+ .upsert ( new Record ( 123L , "hello earth" ) )
246
+ .thenCompose ( v -> ss .upsert ( new Record ( 456L , "hello mars" ) ) )
247
+ )
248
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
249
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
250
+ .thenAccept ( results -> assertThat ( results ).containsExactly (
251
+ new Record ( 123L , "hello earth" ),
252
+ new Record ( 456L , "hello mars" )
253
+ ) )
254
+ )
255
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
256
+ .upsert ( new Record ( 123L , "goodbye earth" ) )
257
+ ) )
258
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
259
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
260
+ .thenAccept ( results -> assertThat ( results ).containsExactly (
261
+ new Record ( 123L , "goodbye earth" ),
262
+ new Record ( 456L , "hello mars" )
263
+ ) )
264
+ )
265
+ );
266
+ }
267
+
268
+
269
+ /**
270
+ * CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
271
+ */
272
+ @ Test
273
+ public void testStageUpsertWithEntityName (VertxTestContext context ) {
274
+ test ( context , getSessionFactory ().withStatelessTransaction ( ss -> ss
275
+ .upsert ( Record .class .getName (), new Record ( 123L , "hello earth" ) )
276
+ .thenCompose ( v -> ss .upsert ( Record .class .getName (), new Record ( 456L , "hello mars" ) ) )
277
+ )
278
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
279
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
280
+ .thenAccept ( results -> assertThat ( results ).containsExactly (
281
+ new Record ( 123L , "hello earth" ),
282
+ new Record ( 456L , "hello mars" )
283
+ ) )
284
+ )
285
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
286
+ .upsert ( Record .class .getName (), new Record ( 123L , "goodbye earth" ) )
287
+ ) )
288
+ .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( ss -> ss
289
+ .createQuery ( "from Record order by id" , Record .class ).getResultList () )
290
+ .thenAccept ( results -> assertThat ( results ).containsExactly (
291
+ new Record ( 123L , "goodbye earth" ),
292
+ new Record ( 456L , "hello mars" )
293
+ ) )
294
+ )
295
+ );
296
+ }
297
+
298
+ @ Entity (name = "Record" )
299
+ @ Table (name = "Record" )
300
+ public static class Record {
301
+ @ Id
302
+ public Long id ;
303
+ public String message ;
304
+
305
+ Record (Long id , String message ) {
306
+ this .id = id ;
307
+ this .message = message ;
308
+ }
309
+
310
+ Record () {
311
+ }
312
+
313
+ public Long getId () {
314
+ return id ;
315
+ }
316
+
317
+ public String getMessage () {
318
+ return message ;
319
+ }
320
+
321
+ public void setMessage (String msg ) {
322
+ message = msg ;
323
+ }
324
+
325
+ // Equals and HashCode for simplifying the test assertions,
326
+ // not to be taken as an example or for production.
327
+ @ Override
328
+ public boolean equals (Object o ) {
329
+ if ( this == o ) {
330
+ return true ;
331
+ }
332
+ if ( o == null || getClass () != o .getClass () ) {
333
+ return false ;
334
+ }
335
+ Record record = (Record ) o ;
336
+ return Objects .equals ( id , record .id ) && Objects .equals ( message , record .message );
337
+ }
338
+
339
+ @ Override
340
+ public int hashCode () {
341
+ return Objects .hash ( id , message );
342
+ }
343
+ }
344
+
209
345
@ NamedQuery (name = "findbyname" , query = "from GuineaPig where name=:n" )
210
346
@ NamedQuery (name = "updatebyname" , query = "update GuineaPig set name='Y'" )
211
347
@ NamedQuery (name = "findall" , query = "from GuineaPig" )
0 commit comments