88import java .util .Collection ;
99import java .util .List ;
1010import java .util .Objects ;
11- import java .util .concurrent .CompletionException ;
1211import javax .persistence .Entity ;
1312import javax .persistence .FetchType ;
1413import javax .persistence .GeneratedValue ;
2120
2221import org .hibernate .Hibernate ;
2322import org .hibernate .LazyInitializationException ;
23+ import org .hibernate .reactive .mutiny .Mutiny ;
2424import org .hibernate .reactive .stage .Stage ;
2525
2626import org .junit .Before ;
2727import org .junit .Test ;
2828
2929import io .vertx .ext .unit .TestContext ;
3030
31+ import static org .assertj .core .api .Assertions .assertThat ;
32+ import static org .hibernate .reactive .testing .ReactiveAssertions .assertThrown ;
3133
32- public class LazyInitializationExceptionWithStage extends BaseReactiveTest {
34+ /**
35+ * We expect to throw the right exception when a lazy initialization error happens.
36+ *
37+ * @see LazyInitializationException
38+ */
39+ public class LazyInitializationExceptionTest extends BaseReactiveTest {
3340
3441 @ Override
3542 protected Collection <Class <?>> annotatedEntities () {
36- return List .of ( Painting .class , Artist .class );
43+ return List .of ( Painting .class , Artist .class );
3744 }
3845
3946 @ Before
@@ -49,39 +56,56 @@ public void populateDB(TestContext context) {
4956 }
5057
5158 @ Test
52- public void testLazyInitializationException (TestContext context ) {
53- test ( context , openSession ()
54- .thenCompose ( session ->
55- session .createQuery ( "from Artist" , Artist .class )
56- .getSingleResult ()
57- .thenAccept ( artist -> artist .getPaintings ().size () )
58- .handle ( (ignore , throwable ) -> {
59- if (throwable == null ) {
60- context .fail ( "Unexpected success, we expect "
61- + LazyInitializationException .class .getName () );
62- }
63- else {
64- context .assertEquals ( CompletionException .class , throwable .getClass () );
65- context .assertEquals ( LazyInitializationException .class , throwable .getCause ().getClass () );
66- context .assertTrue (
67- throwable .getMessage ().startsWith (
68- "org.hibernate.LazyInitializationException: HR000056: Collection cannot be initialized: org.hibernate.reactive.LazyInitializationExceptionWithStage$Artist.paintings" )
69- );
70- }
71- return null ;
72- } )
73- ) );
59+ public void testLazyInitializationExceptionWithMutiny (TestContext context ) {
60+ test ( context , assertThrown ( LazyInitializationException .class , openMutinySession ()
61+ .chain ( ms -> ms .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
62+ .invoke ( artist -> artist .getPaintings ().size () ) )
63+ .invoke ( LazyInitializationExceptionTest ::assertLazyInitialization )
64+ );
65+ }
66+
67+ @ Test
68+ public void testLazyInitializationExceptionWithStage (TestContext context ) {
69+ test ( context , assertThrown ( LazyInitializationException .class , openSession ()
70+ .thenCompose ( ss -> ss .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
71+ .thenAccept ( artist -> artist .getPaintings ().size () ) )
72+ .thenAccept ( LazyInitializationExceptionTest ::assertLazyInitialization )
73+ );
74+ }
75+
76+ private static void assertLazyInitialization (LazyInitializationException e ) {
77+ assertThat ( e .getMessage () )
78+ .startsWith ( "HR000056: Collection cannot be initialized: " + Artist .class .getName () + ".paintings" );
79+ }
80+
81+ @ Test
82+ public void testLazyInitializationExceptionNotThrownWithMutiny (TestContext context ) {
83+ test ( context , openMutinySession ()
84+ .chain ( session -> session .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
85+ // We are checking `.getPaintings()` but not doing anything with it and therefore it should work.
86+ .invoke ( Artist ::getPaintings )
87+ );
7488 }
7589
7690 @ Test
77- public void testLazyInitializationExceptionNotThrown (TestContext context ) {
91+ public void testLazyInitializationExceptionNotThrownWithStage (TestContext context ) {
7892 test ( context , openSession ()
79- .thenCompose ( session -> session .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
80- // We are checking `.getPaintings()` but not doing anything with it and therefore it should work.
81- .thenAccept ( Artist ::getPaintings )
93+ .thenCompose ( session -> session .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
94+ // We are checking `.getPaintings()` but not doing anything with it and therefore it should work.
95+ .thenAccept ( Artist ::getPaintings )
8296 );
8397 }
8498
99+ @ Test
100+ public void testLazyInitializationWithJoinFetchAndMutiny (TestContext context ) {
101+ test ( context , openMutinySession ()
102+ .chain ( session -> session .createQuery ( "from Artist a join fetch a.paintings" , Artist .class ).getSingleResult () )
103+ .onItem ().invoke ( artist -> {
104+ context .assertTrue ( Hibernate .isInitialized ( artist ) );
105+ context .assertEquals ( 2 , artist .getPaintings ().size () );
106+ } ) );
107+ }
108+
85109 @ Test
86110 public void testLazyInitializationWithJoinFetch (TestContext context ) {
87111 test ( context , openSession ()
@@ -94,6 +118,19 @@ public void testLazyInitializationWithJoinFetch(TestContext context) {
94118 } ) );
95119 }
96120
121+ @ Test
122+ public void testLazyInitializationWithMutinyFetch (TestContext context ) {
123+ test ( context , openMutinySession ()
124+ .chain ( session -> session .createQuery ( "from Artist" , Artist .class ).getSingleResult () )
125+ .chain ( artist -> Mutiny .fetch ( artist .paintings )
126+ .invoke ( paintings -> {
127+ context .assertTrue ( Hibernate .isInitialized ( paintings ) );
128+ context .assertEquals ( 2 , paintings .size () );
129+ } )
130+ )
131+ );
132+ }
133+
97134 @ Test
98135 public void testLazyInitializationWithStageFetch (TestContext context ) {
99136 test ( context , openSession ()
0 commit comments