8
8
import java .util .Collection ;
9
9
import java .util .List ;
10
10
import java .util .Objects ;
11
- import java .util .concurrent .CompletionException ;
12
11
import javax .persistence .Entity ;
13
12
import javax .persistence .FetchType ;
14
13
import javax .persistence .GeneratedValue ;
21
20
22
21
import org .hibernate .Hibernate ;
23
22
import org .hibernate .LazyInitializationException ;
23
+ import org .hibernate .reactive .mutiny .Mutiny ;
24
24
import org .hibernate .reactive .stage .Stage ;
25
25
26
26
import org .junit .Before ;
27
27
import org .junit .Test ;
28
28
29
29
import io .vertx .ext .unit .TestContext ;
30
30
31
+ import static org .assertj .core .api .Assertions .assertThat ;
32
+ import static org .hibernate .reactive .testing .ReactiveAssertions .assertThrown ;
31
33
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 {
33
40
34
41
@ Override
35
42
protected Collection <Class <?>> annotatedEntities () {
36
- return List .of ( Painting .class , Artist .class );
43
+ return List .of ( Painting .class , Artist .class );
37
44
}
38
45
39
46
@ Before
@@ -49,39 +56,56 @@ public void populateDB(TestContext context) {
49
56
}
50
57
51
58
@ 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
+ );
74
88
}
75
89
76
90
@ Test
77
- public void testLazyInitializationExceptionNotThrown (TestContext context ) {
91
+ public void testLazyInitializationExceptionNotThrownWithStage (TestContext context ) {
78
92
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 )
82
96
);
83
97
}
84
98
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
+
85
109
@ Test
86
110
public void testLazyInitializationWithJoinFetch (TestContext context ) {
87
111
test ( context , openSession ()
@@ -94,6 +118,19 @@ public void testLazyInitializationWithJoinFetch(TestContext context) {
94
118
} ) );
95
119
}
96
120
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
+
97
134
@ Test
98
135
public void testLazyInitializationWithStageFetch (TestContext context ) {
99
136
test ( context , openSession ()
0 commit comments