Skip to content

Update javadoc for .fetch + tests #1894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -848,16 +848,23 @@ default Uni<Void> lock(Object entity, LockModeType lockModeType) {

/**
* Asynchronously fetch an association that's configured for lazy loading.
*
* <p>
* <pre>
* {@code session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()));}
* </pre>
*
* @param association a lazy-loaded association
* </p>
* <p>
* It can also initialize proxys. For example:
* <pre>
* {@code session.fetch(session.getReference(Author.class, authorId))}
* </pre>
* </p>
* @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)
*/
<T> Uni<T> fetch(T association);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,24 @@ default CompletionStage<Void> lock(Object entity, LockModeType lockModeType) {

/**
* Asynchronously fetch an association that's configured for lazy loading.
*
* <p>
* <pre>
* {@code session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))}
* </pre>
* </p>
* <p>
* It can also initialize proxys. For example:
* <pre>
* {@code session.fetch(session.getReference(Author.class, authorId))}
* </pre>
* </p>
*
* @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)
*/
<T> CompletionStage<T> fetch(T association);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) )
) )
);
}
}
Loading