Skip to content

Commit a241992

Browse files
committed
[#1770] Add test for Better error message when transparently loading lazy fields
1 parent 793a30b commit a241992

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyPropertyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public Book(String isbn, String title, Author author) {
153153
}
154154

155155
public Book() {
156-
super( new EntityRelatedState( "Book", singleton( "isbn" ) ), 1, null );
156+
super( new EntityRelatedState( Book.class.getName(), singleton( "isbn" ) ), 1, null );
157157
}
158158

159159
@Override

integration-tests/bytecode-enhancements-it/src/test/java/org/hibernate/reactive/it/LazyBasicFieldTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
package org.hibernate.reactive.it;
77

8+
import org.hibernate.LazyInitializationException;
9+
10+
import io.smallrye.mutiny.Uni;
811
import java.util.Collection;
912
import java.util.List;
1013
import java.util.concurrent.TimeUnit;
@@ -47,4 +50,46 @@ public void testFetchBasicField(VertxTestContext context) {
4750
) ) )
4851
);
4952
}
53+
54+
@Test
55+
public void testTransparentLazyFetching(VertxTestContext context) {
56+
final Crew emily = new Crew();
57+
emily.setId( 21L );
58+
emily.setName( "Emily Jackson" );
59+
emily.setRole( "Passenger" );
60+
emily.setFate( "Unknown" );
61+
62+
test( context, assertThrown( LazyInitializationException.class, getMutinySessionFactory()
63+
.withTransaction( session -> session.persist( emily ) )
64+
.call( () -> getMutinySessionFactory().withSession( session -> session.find( Crew.class, emily.getId() )
65+
.invoke( Crew::getRole ) ) )
66+
).invoke( exception -> assertThat( exception.getMessage() ).contains( "Reactive sessions do not support transparent lazy fetching" ) )
67+
);
68+
}
69+
70+
@Test
71+
public void testGetReferenceAndTransparentLazyFetching(VertxTestContext context) {
72+
final Crew emily = new Crew();
73+
emily.setId( 21L );
74+
emily.setName( "Emily Jackson" );
75+
emily.setRole( "Passenger" );
76+
emily.setFate( "Unknown" );
77+
78+
test( context, assertThrown( LazyInitializationException.class, getMutinySessionFactory()
79+
.withTransaction( session -> session.persist( emily ) )
80+
.chain( () -> getMutinySessionFactory().withSession( session -> {
81+
Crew crew = session.getReference( Crew.class, emily.getId() );
82+
String role = crew.getRole();
83+
return session.flush();
84+
} ) )
85+
).invoke( exception -> assertThat( exception.getMessage() ).contains( "Reactive sessions do not support transparent lazy fetching" ) )
86+
);
87+
}
88+
89+
public static <U extends Throwable> Uni<U> assertThrown(Class<U> expectedException, Uni<?> uni) {
90+
return uni.onItemOrFailure().transform( (s, e) -> {
91+
assertThat( e ).isInstanceOf( expectedException );
92+
return (U) e;
93+
} );
94+
}
5095
}

0 commit comments

Comments
 (0)