1818import javax .persistence .OneToMany ;
1919import javax .persistence .Table ;
2020
21+ import org .hibernate .Hibernate ;
2122import org .hibernate .LazyInitializationException ;
23+ import org .hibernate .reactive .mutiny .Mutiny ;
2224
2325import org .junit .Before ;
2426import org .junit .Test ;
@@ -31,15 +33,21 @@ public class LazyInitializationExceptionWithMutiny extends BaseReactiveTest {
3133
3234 @ Override
3335 protected Collection <Class <?>> annotatedEntities () {
34- return List .of ( Artist .class , Painting .class );
36+ return List .of ( Painting .class , Artist .class );
3537 }
3638
3739 @ Before
3840 public void populateDB (TestContext context ) {
3941 Async async = context .async ();
4042 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" );
4149 getMutinySessionFactory ()
42- .withTransaction ( (session , tx ) -> session .persist ( artemisia ) )
50+ .withTransaction ( (session , tx ) -> session .persistAll ( artemisia , liuto , sev ) )
4351 .subscribe ().with ( success -> async .complete (), context ::fail );
4452 }
4553
@@ -53,9 +61,9 @@ public void testLazyInitializationException(TestContext context) {
5361 .onItem ().invoke ( () -> context .fail ( "Unexpected success, we expect " + LazyInitializationException .class .getName () ) )
5462 .onFailure ().recoverWithUni ( throwable -> {
5563 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" )
5967 );
6068 return Uni .createFrom ().nullItem ();
6169 } )
@@ -73,6 +81,33 @@ public void testLazyInitializationExceptionNotThrown(TestContext context) {
7381 );
7482 }
7583
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+
76111 @ Entity (name = "Painting" )
77112 @ Table (name = "painting" )
78113 public static class Painting {
@@ -186,10 +221,6 @@ public List<Painting> getPaintings() {
186221 return paintings ;
187222 }
188223
189- public void setPaintings (List <Painting > paintings ) {
190- this .paintings = paintings ;
191- }
192-
193224 @ Override
194225 public boolean equals (Object o ) {
195226 if ( this == o ) {
0 commit comments