|
5 | 5 | */
|
6 | 6 | package org.hibernate.reactive.it;
|
7 | 7 |
|
| 8 | +import org.hibernate.LazyInitializationException; |
| 9 | + |
| 10 | +import io.smallrye.mutiny.Uni; |
8 | 11 | import java.util.Collection;
|
9 | 12 | import java.util.List;
|
10 | 13 | import java.util.concurrent.TimeUnit;
|
@@ -47,4 +50,46 @@ public void testFetchBasicField(VertxTestContext context) {
|
47 | 50 | ) ) )
|
48 | 51 | );
|
49 | 52 | }
|
| 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 | + } |
50 | 95 | }
|
0 commit comments