18
18
import javax .persistence .OneToMany ;
19
19
import javax .persistence .Table ;
20
20
21
+ import org .hibernate .Hibernate ;
21
22
import org .hibernate .LazyInitializationException ;
23
+ import org .hibernate .reactive .mutiny .Mutiny ;
22
24
23
25
import org .junit .Before ;
24
26
import org .junit .Test ;
@@ -31,15 +33,21 @@ public class LazyInitializationExceptionWithMutiny extends BaseReactiveTest {
31
33
32
34
@ Override
33
35
protected Collection <Class <?>> annotatedEntities () {
34
- return List .of ( Artist .class , Painting .class );
36
+ return List .of ( Painting .class , Artist .class );
35
37
}
36
38
37
39
@ Before
38
40
public void populateDB (TestContext context ) {
39
41
Async async = context .async ();
40
42
Artist artemisia = new Artist ( "Artemisia Gentileschi" );
43
+ Painting sev = new Painting ();
44
+ sev .setAuthor ( artemisia );
45
+ sev .setName ( "Susanna e i vecchioni" );
46
+ Painting liuto = new Painting ();
47
+ liuto .setAuthor ( artemisia );
48
+ liuto .setName ( "Autoritratto come suonatrice di liuto" );
41
49
getMutinySessionFactory ()
42
- .withTransaction ( (session , tx ) -> session .persist ( artemisia ) )
50
+ .withTransaction ( (session , tx ) -> session .persistAll ( artemisia , liuto , sev ) )
43
51
.subscribe ().with ( success -> async .complete (), context ::fail );
44
52
}
45
53
@@ -53,9 +61,9 @@ public void testLazyInitializationException(TestContext context) {
53
61
.onItem ().invoke ( () -> context .fail ( "Unexpected success, we expect " + LazyInitializationException .class .getName () ) )
54
62
.onFailure ().recoverWithUni ( throwable -> {
55
63
context .assertEquals ( LazyInitializationException .class , throwable .getClass () );
56
- context .assertEquals (
57
- "HR000056: Collection cannot be initialized: org.hibernate.reactive.LazyInitializationExceptionWithMutiny$Artist.paintings" ,
58
- throwable . getMessage ( )
64
+ context .assertTrue (
65
+ throwable . getMessage (). startsWith (
66
+ "HR000056: Collection cannot be initialized: org.hibernate.reactive.LazyInitializationExceptionWithMutiny$Artist.paintings" )
59
67
);
60
68
return Uni .createFrom ().nullItem ();
61
69
} )
@@ -73,6 +81,33 @@ public void testLazyInitializationExceptionNotThrown(TestContext context) {
73
81
);
74
82
}
75
83
84
+ @ Test
85
+ public void testLazyInitializationWithJoinFetch (TestContext context ) {
86
+ test ( context , openMutinySession ()
87
+ .chain ( session -> session
88
+ .createQuery ( "from Artist a join fetch a.paintings" , Artist .class )
89
+ .getSingleResult () )
90
+ .onItem ().invoke ( artist -> {
91
+ context .assertTrue ( Hibernate .isInitialized ( artist ) );
92
+ context .assertEquals ( 2 , artist .getPaintings ().size () );
93
+ } ) );
94
+ }
95
+
96
+ @ Test
97
+ public void testLazyInitializationWithMutinyFetch (TestContext context ) {
98
+ test ( context ,
99
+ openMutinySession ().chain ( session -> session
100
+ .createQuery ( "from Artist" , Artist .class )
101
+ .getSingleResult () )
102
+ .chain ( artist -> Mutiny .fetch ( artist .paintings )
103
+ .invoke ( paintings -> {
104
+ context .assertTrue ( Hibernate .isInitialized ( paintings ) );
105
+ context .assertEquals ( 2 , paintings .size () );
106
+ } )
107
+ )
108
+ );
109
+ }
110
+
76
111
@ Entity (name = "Painting" )
77
112
@ Table (name = "painting" )
78
113
public static class Painting {
@@ -186,10 +221,6 @@ public List<Painting> getPaintings() {
186
221
return paintings ;
187
222
}
188
223
189
- public void setPaintings (List <Painting > paintings ) {
190
- this .paintings = paintings ;
191
- }
192
-
193
224
@ Override
194
225
public boolean equals (Object o ) {
195
226
if ( this == o ) {
0 commit comments