8000 HHH-19468 enableFetchProfile() should accept an EnabledFetchProfile by gavinking · Pull Request #10162 · hibernate/hibernate-orm · GitHub
[go: up one dir, main page]

Skip to content

HHH-19468 enableFetchProfile() should accept an EnabledFetchProfile #10162

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion documentation/src/main/asciidoc/introduction/Advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1144,10 +1144,17 @@ A fetch profile must be explicitly enabled for a given session by calling link:{

[source,java]
----
session.enableFetchProfile(Book_.PROFILE_EAGER_BOOK);
session.enableFetchProfile(Book_._EagerBook);
Book eagerBook = session.find(Book.class, bookId);
----

Alternatively, a profile may be passed as a `FindOption`:

[source,java]
----
Book eagerBook = session.find(Book.class, bookId, Book_._EagerBook);
----

So why or when might we prefer named fetch profiles to entity graphs?
Well, it's really hard to say.
It's nice that this feature _exists_, and if you love it, that's great.
Expand Down
22 changes: 21 additions & 1 deletion hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ public interface Session extends SharedSessionContract, EntityManager {
boolean isFetchProfileEnabled(String name) throws UnknownProfileException;

/**
* Enable the {@link org.hibernate.annotations.FetchProfile fetch profile}
* Enable the {@linkplain org.hibernate.annotations.FetchProfile fetch profile}
* with the given name in this session. If the requested fetch profile is
* already enabled, the call has no effect.
*
Expand All @@ -1288,6 +1288,26 @@ public interface Session extends SharedSessionContract, EntityManager {
*/
void enableFetchProfile(String name) throws UnknownProfileException;

/**
* Enable the {@linkplain org.hibernate.annotations.FetchProfile fetch profile}
* represented by the given instance of {@link EnabledFetchProfile}. If
* the requested fetch profile is already enabled, the call has no effect.
* <p>
* An instance of {@link EnabledFetchProfile} may be obtained in a typesafe
* way from the static metamodel.
*
* @param fetchProfile the {@link EnabledFetchProfile}
*
* @throws UnknownProfileException Indicates that the given object has
* a profile name which does not match
* any known fetch profile names
*
* @see org.hibernate.annotations.FetchProfile
*
* @since 7
*/
void enableFetchProfile(EnabledFetchProfile fetchProfile) throws UnknownProfileException;

/**
* Disable the {@link org.hibernate.annotations.FetchProfile fetch profile}
* with the given name in this session. If the requested fetch profile is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.persistence.metamodel.Metamodel;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
Expand Down Expand Up @@ -1102,6 +1103,11 @@ public void enableFetchProfile(String name) throws UnknownProfileException {
delegate.enableFetchProfile( name );
}

@Override
public void enableFetchProfile(EnabledFetchProfile fetchProfile) {
delegate.enableFetchProfile( fetchProfile );
}

@Override
public void disableFetchProfile(String name) throws UnknownProfileException {
delegate.disableFetchProfile( name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.persistence.metamodel.Metamodel;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
Expand Down Expand Up @@ -416,6 +417,11 @@ public void enableFetchProfile(String name) throws UnknownProfileException {
this.lazySession.get().enableFetchProfile( name );
}

@Override
public void enableFetchProfile(EnabledFetchProfile fetchProfile) {
this.lazySession.get().enableFetchProfile( fetchProfile );
}

@Override
public void disableFetchProfile(String name) throws UnknownProfileException {
this.lazySession.get().disableFetchProfile( name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,11 @@ public void enableFetchProfile(String name) throws UnknownProfileException {
loadQueryInfluencers.enableFetchProfile( name );
}

@Override
public void enableFetchProfile(EnabledFetchProfile fetchProfile) {
enableFetchProfile( fetchProfile.profileName() );
}

@Override
public void disableFetchProfile(String name) throws UnknownProfileException {
loadQueryInfluencers.disableFetchProfile( name );
Expand Down
4 changes: 4 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.persistence.PessimisticLockScope;
import jakarta.persistence.Timeout;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.Incubating;
import org.hibernate.LockMode;
Expand Down Expand Up @@ -946,6 +947,9 @@ default Query<R> setPage(Page page) {
@Override
Query<R> setEntityGraph(EntityGraph<? super R> graph, GraphSemantic semantic);

@Override
Query<R> enableFetchProfile(EnabledFetchProfile fetchProfile);

@Override
Query<R> enableFetchProfile(String profileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.persistence.EntityGraph;
import jakarta.persistence.PessimisticLockScope;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.Incubating;
import org.hibernate.LockMode;
Expand Down Expand Up @@ -286,6 +287,30 @@ default Stream<R> stream() {
*/
SelectionQuery<R> setEntityGraph(EntityGraph<? super R> graph, GraphSemantic semantic);

/**
* Enable the {@linkplain org.hibernate.annotations.FetchProfile fetch
* profile} represented by the given instance of {@link EnabledFetchProfile}
* during execution of this query. If the requested fetch profile is already
* enabled, the call has no effect.
* <p>
* This is an alternative way to specify the associations which
* should be fetched as part of the initial query.
* <p>
* An instance of {@link EnabledFetchProfile} may be obtained in a typesafe
* way from the static metamodel.
*
* @param fetchProfile the {@link EnabledFetchProfile}
*
* @throws UnknownProfileException Indicates that the given object has
* a profile name which does not match
* any known fetch profile names
*
* @see org.hibernate.annotations.FetchProfile
*
* @since 7
*/
SelectionQuery<R> enableFetchProfile(EnabledFetchProfile fetchProfile);

/**
* Enable the {@linkplain org.hibernate.annotations.FetchProfile fetch
* profile} with the given name during execution of this query. If the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import jakarta.persistence.Timeout;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.query.QueryFlushMode;
import org.hibernate.HibernateException;
Expand Down Expand Up @@ -127,6 +128,12 @@ public QueryImplementor<R> setEntityGraph(EntityGraph<? super R> graph, GraphSem
return this;
}

@Override
public QueryImplementor<R> enableFetchProfile(EnabledFetchProfile fetchProfile) {
super.enableFetchProfile( fetchProfile );
return this;
}

@Override
public QueryImplementor<R> enableFetchProfile(String profileName) {
super.enableFetchProfile( profileName );
Expand Down
import jakarta.persistence.Timeout;
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.persistence.PessimisticLockScope;
import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.ScrollableResults;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down Expand Up @@ -380,6 +381,11 @@ public SelectionQuery<R> setEntityGraph(EntityGraph<? super R> graph, GraphSeman
return this;
}

@Override
public SelectionQuery<R> enableFetchProfile(EnabledFetchProfile fetchProfile) {
return enableFetchProfile( fetchProfile.profileName() );
}

@Override
public SelectionQuery<R> enableFetchProfile(String profileName) {
if ( this.getSessionFactory().containsFetchProfileDefinition( profileName ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.persistence.metamodel.Type;

import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
Expand Down Expand Up @@ -847,6 +848,12 @@ public Query<R> setEntityGraph(EntityGraph<? super R> graph, GraphSemantic seman
return this;
}

@Override
public Query<R> enableFetchProfile(EnabledFetchProfile fetchProfile) {
super.enableFetchProfile( fetchProfile );
return this;
}

@Override
public Query<R> enableFetchProfile(String profileName) {
super.enableFetchProfile( profileName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.stream.Stream;

import org.hibernate.CacheMode;
import org.hibernate.EnabledFetchProfile;
import org.hibernate.FlushMode;
import org.hibernate.Incubating;
import org.hibernate.LockMode;
Expand All @@ -28,6 +29,7 @@
import org.hibernate.query.QueryFlushMode;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.SqmSelectionQuery;
Expand Down Expand Up @@ -164,6 +166,12 @@ public SqmSelectionQueryImplementor<R> setEntityGraph(EntityGraph<? super R> gra
return this;
}

@Override
public SelectionQuery<R> enableFetchProfile(EnabledFetchProfile fetchProfile) {
getDelegate().enableFetchProfile( fetchProfile );
return this;
}

@Override
public SqmSelectionQueryImplementor<R> enableFetchProfile(String profileName) {
getDelegate().enableFetchProfile( profileName );
Expand Down
0