From 91861b9be2e6ead128de625e3726304984bcb9f5 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Tue, 23 Apr 2024 16:48:58 +0200 Subject: [PATCH 1/2] [#1893] Add test cases for fetch(getReference()) --- .../org/hibernate/reactive/ReferenceTest.java | 20 +++++++++++++++++++ .../reactive/it/ReferenceBETest.java | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java index 9318d057c..ac6da727a 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java @@ -30,6 +30,7 @@ import jakarta.persistence.Version; import static java.util.concurrent.TimeUnit.MINUTES; +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.reactive.util.impl.CompletionStages.loop; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -231,6 +232,25 @@ public void testRefreshDetachedProxy(VertxTestContext context) { ); } + @Test + public void testFetchOfReference(VertxTestContext context) { + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); + final Author neil = new Author( "Neil Gaiman", goodOmens ); + final Author terry = new Author( "Terry Pratchett", goodOmens ); + goodOmens.getAuthors().add( neil ); + goodOmens.getAuthors().add( terry ); + + test( context, getMutinySessionFactory() + .withTransaction( s -> s.persistAll( goodOmens, terry, neil ) ) + .call( () -> getMutinySessionFactory().withSession( s -> s + // Not the most common case, but should be possible + .fetch( s.getReference( Book.class, goodOmens.getId() ) ) + .chain( reference -> s.fetch( reference.getAuthors() ) ) + .invoke( authors -> assertThat( authors ).containsExactlyInAnyOrder( terry, neil ) ) + ) ) + ); + } + @Entity(name = "Tome") @Table(name = "TBook") public static class Book { diff --git a/integration-tests/bytecode-enhancements-it/src/test/java/org/hibernate/reactive/it/ReferenceBETest.java b/integration-tests/bytecode-enhancements-it/src/test/java/org/hibernate/reactive/it/ReferenceBETest.java index 034b118e7..49b6d73bf 100644 --- a/integration-tests/bytecode-enhancements-it/src/test/java/org/hibernate/reactive/it/ReferenceBETest.java +++ b/integration-tests/bytecode-enhancements-it/src/test/java/org/hibernate/reactive/it/ReferenceBETest.java @@ -22,6 +22,7 @@ import io.vertx.junit5.VertxTestContext; import static java.util.concurrent.TimeUnit.MINUTES; +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture; import static org.hibernate.reactive.util.impl.CompletionStages.loop; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -212,4 +213,23 @@ public void testRefreshDetachedProxy(VertxTestContext context) { ) ) ); } + + @Test + public void testFetchOfReference(VertxTestContext context) { + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); + final Author neil = new Author( "Neil Gaiman", goodOmens ); + final Author terry = new Author( "Terry Pratchett", goodOmens ); + goodOmens.getAuthors().add( neil ); + goodOmens.getAuthors().add( terry ); + + test( context, getMutinySessionFactory() + .withTransaction( s -> s.persistAll( goodOmens, terry, neil ) ) + .call( () -> getMutinySessionFactory().withSession( s -> s + // Not the most common case, but should be possible + .fetch( s.getReference( Book.class, goodOmens.getId() ) ) + .chain( reference -> s.fetch( reference.getAuthors() ) ) + .invoke( authors -> assertThat( authors ).containsExactlyInAnyOrder( terry, neil ) ) + ) ) + ); + } } From 738266815dc067e5bf04b9ef85240be3cf0fb1fd Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Tue, 23 Apr 2024 16:56:07 +0200 Subject: [PATCH 2/2] [#1893] Update `.fetch` javadoc --- .../java/org/hibernate/reactive/mutiny/Mutiny.java | 13 ++++++++++--- .../java/org/hibernate/reactive/stage/Stage.java | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java index 36e1f25f2..22ec48da1 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java @@ -848,16 +848,23 @@ default Uni lock(Object entity, LockModeType lockModeType) { /** * Asynchronously fetch an association that's configured for lazy loading. - * + *

*

 		 * {@code session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()));}
 		 * 
- * - * @param association a lazy-loaded association + *

+ *

+ * It can also initialize proxys. For example: + *

+		 * {@code session.fetch(session.getReference(Author.class, authorId))}
+		 * 
+ *

+ * @param association a lazy-loaded association, or a proxy * * @return the fetched association, via a {@code Uni} * * @see Mutiny#fetch(Object) + * @see #getReference(Class, Object) * @see org.hibernate.Hibernate#initialize(Object) */ Uni fetch(T association); diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java index 5016bcfc3..5fc1d671e 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java @@ -877,16 +877,24 @@ default CompletionStage lock(Object entity, LockModeType lockModeType) { /** * Asynchronously fetch an association that's configured for lazy loading. - * + *

*

 		 * {@code session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))}
 		 * 
+ *

+ *

+ * It can also initialize proxys. For example: + *

+		 * {@code session.fetch(session.getReference(Author.class, authorId))}
+		 * 
+ *

* - * @param association a lazy-loaded association + * @param association a lazy-loaded association, or a proxy * * @return the fetched association, via a {@code CompletionStage} * * @see Stage#fetch(Object) + * @see #getReference(Class, Object) * @see org.hibernate.Hibernate#initialize(Object) */ CompletionStage fetch(T association);