From 3be2dc5978db063ee3e1e8f8829ad147dcb8617f Mon Sep 17 00:00:00 2001 From: Gavin Date: Tue, 23 May 2023 01:12:08 +0200 Subject: [PATCH] HHH-16651 promote new operations from SessionBuilder to Session setFetchBatchSize() and setSubselectFetchEnabled() --- .../src/main/java/org/hibernate/Session.java | 53 +++++++++++++++++++ .../java/org/hibernate/SessionBuilder.java | 4 -- .../org/hibernate/SharedSessionBuilder.java | 10 ---- .../org/hibernate/SharedSessionContract.java | 7 +-- .../org/hibernate/cfg/AvailableSettings.java | 2 + .../spi/AbstractDelegatingSessionBuilder.java | 12 ----- ...bstractDelegatingSharedSessionBuilder.java | 24 --------- .../engine/spi/SessionDelegatorBaseImpl.java | 20 +++++++ .../engine/spi/SessionLazyDelegator.java | 20 +++++++ .../internal/SessionFactoryImpl.java | 13 ----- .../org/hibernate/internal/SessionImpl.java | 32 +++-------- 11 files changed, 106 insertions(+), 91 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Session.java b/hibernate-core/src/main/java/org/hibernate/Session.java index ccba71529b..da6f923a21 100644 --- a/hibernate-core/src/main/java/org/hibernate/Session.java +++ b/hibernate-core/src/main/java/org/hibernate/Session.java @@ -272,6 +272,59 @@ public interface Session extends SharedSessionContract, EntityManager { */ void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode); + /** + * Get the maximum batch size for batch fetching associations by + * id in this session. + * + * @since 6.3 + */ + int getFetchBatchSize(); + + /** + * Set the maximum batch size for batch fetching associations by + * id in this session. Override the + * {@linkplain org.hibernate.boot.spi.SessionFactoryOptions#getDefaultBatchFetchSize() + * factory-level} default controlled by the configuration property + * {@value org.hibernate.cfg.AvailableSettings#DEFAULT_BATCH_FETCH_SIZE}. + *

+ *

+ * + * @param batchSize the maximum batch size for batch fetching + * + * @since 6.3 + * + * @see org.hibernate.cfg.AvailableSettings#DEFAULT_BATCH_FETCH_SIZE + */ + void setFetchBatchSize(int batchSize); + + /** + * Determine if subselect fetching is enabled in this session. + * + * @return {@code true} is subselect fetching is enabled + * + * @since 6.3 + */ + boolean isSubselectFetchingEnabled(); + + /** + * Enable or disable subselect fetching in this session. Override the + * {@linkplain org.hibernate.boot.spi.SessionFactoryOptions#isSubselectFetchEnabled() + * factory-level} default controlled by the configuration property + * {@value org.hibernate.cfg.AvailableSettings#USE_SUBSELECT_FETCH}. + * + * @param enabled {@code true} to enable subselect fetching + * + * @since 6.3 + * + * @see org.hibernate.cfg.AvailableSettings#USE_SUBSELECT_FETCH + */ + void setSubselectFetchingEnabled(boolean enabled); + /** * Get the session factory which created this session. * diff --git a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java index 307af37ca4..458450ad2a 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java @@ -145,8 +145,4 @@ public interface SessionBuilder { * @see jakarta.persistence.PersistenceContextType */ SessionBuilder autoClose(boolean autoClose); - - SessionBuilder defaultBatchFetchSize(int batchSize); - - SessionBuilder subselectFetchEnabled(boolean enabled); } diff --git a/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java index 062dcda487..4d188263ba 100644 --- a/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java @@ -60,10 +60,6 @@ public interface SharedSessionBuilder extends SessionBuilder { */ SharedSessionBuilder flushMode(); - SharedSessionBuilder defaultBatchFetchSize(); - - SharedSessionBuilder subselectFetchEnabled(); - /** * Signifies that the autoClose flag from the original session should be used to create the new session. * @@ -88,10 +84,4 @@ public interface SharedSessionBuilder extends SessionBuilder { @Override SharedSessionBuilder autoClose(boolean autoClose); - - @Override - SharedSessionBuilder defaultBatchFetchSize(int batchSize); - - @Override - SharedSessionBuilder subselectFetchEnabled(boolean enabled); } diff --git a/hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java index 2d6980f38f..43041e19f7 100644 --- a/hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java @@ -185,15 +185,16 @@ public interface SharedSessionContract extends QueryProducer, Closeable, Seriali Integer getJdbcBatchSize(); /** - * Set the session-level JDBC batch size. Overrides the - * {@link org.hibernate.boot.spi.SessionFactoryOptions#getJdbcBatchSize() factory-level} - * JDBC batch size defined by the configuration property + * Set the session-level JDBC batch size. Override the + * {@linkplain org.hibernate.boot.spi.SessionFactoryOptions#getJdbcBatchSize() factory-level} + * JDBC batch size controlled by the configuration property * {@value org.hibernate.cfg.AvailableSettings#STATEMENT_BATCH_SIZE}. * * @param jdbcBatchSize the new session-level JDBC batch size * * @since 5.2 * + * @see org.hibernate.cfg.AvailableSettings#STATEMENT_BATCH_SIZE * @see org.hibernate.boot.spi.SessionFactoryOptions#getJdbcBatchSize * @see org.hibernate.boot.SessionFactoryBuilder#applyJdbcBatchSize */ diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 0950a85d8f..c2a70a1a7c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -1033,6 +1033,7 @@ public interface AvailableSettings { * collections explicitly annotated {@code @BatchSize}. * * @see org.hibernate.annotations.BatchSize + * @see org.hibernate.Session#setFetchBatchSize(int) * @see org.hibernate.boot.SessionFactoryBuilder#applyDefaultBatchFetchSize(int) */ String DEFAULT_BATCH_FETCH_SIZE = "hibernate.default_batch_fetch_size"; @@ -1045,6 +1046,7 @@ public interface AvailableSettings { * explicitly annotated {@code @Fetch(SUBSELECT)}. * * @see org.hibernate.annotations.FetchMode#SUBSELECT + * @see org.hibernate.Session#setSubselectFetchingEnabled(boolean) * @see org.hibernate.boot.SessionFactoryBuilder#applySubselectFetchEnabled(boolean) */ String USE_SUBSELECT_FETCH = "hibernate.use_subselect_fetch"; diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java index 2def4482b5..fa8df90e8f 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java @@ -122,16 +122,4 @@ public abstract class AbstractDelegatingSessionBuilder implements SessionBuilder delegate.flushMode( flushMode ); return this; } - - @Override - public SessionBuilder defaultBatchFetchSize(int batchSize) { - delegate.defaultBatchFetchSize( batchSize ); - return this; - } - - @Override - public SessionBuilder subselectFetchEnabled(boolean enabled) { - delegate.subselectFetchEnabled( enabled ); - return this; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java index 43b315bc25..d08232a6c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java @@ -164,28 +164,4 @@ public abstract class AbstractDelegatingSharedSessionBuilder implements SharedSe delegate.jdbcTimeZone( timeZone ); return this; } - - @Override - public SharedSessionBuilder defaultBatchFetchSize(int batchSize) { - delegate.defaultBatchFetchSize( batchSize ); - return this; - } - - @Override - public SharedSessionBuilder subselectFetchEnabled(boolean enabled) { - delegate.subselectFetchEnabled( enabled ); - return this; - } - - @Override - public SharedSessionBuilder defaultBatchFetchSize() { - delegate.defaultBatchFetchSize(); - return this; - } - - @Override - public SharedSessionBuilder subselectFetchEnabled() { - delegate.subselectFetchEnabled(); - return this; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index d13ca22417..e0d7763226 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -1201,6 +1201,26 @@ public class SessionDelegatorBaseImpl implements SessionImplementor { delegate.setJdbcBatchSize( jdbcBatchSize ); } + @Override + public boolean isSubselectFetchingEnabled() { + return delegate.isSubselectFetchingEnabled(); + } + + @Override + public void setSubselectFetchingEnabled(boolean enabled) { + delegate.setSubselectFetchingEnabled( enabled ); + } + + @Override + public int getFetchBatchSize() { + return delegate.getFetchBatchSize(); + } + + @Override + public void setFetchBatchSize(int batchSize) { + delegate.setFetchBatchSize( batchSize ); + } + @Override public TimeZone getJdbcTimeZone() { return delegate.getJdbcTimeZone(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java index 6ebbac067b..f6a3ffcb48 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java @@ -659,6 +659,26 @@ public class SessionLazyDelegator implements Session { this.lazySession.get().setJdbcBatchSize( jdbcBatchSize ); } + @Override + public int getFetchBatchSize() { + return this.lazySession.get().getFetchBatchSize(); + } + + @Override + public void setFetchBatchSize(int batchSize) { + this.lazySession.get().setFetchBatchSize( batchSize ); + } + + @Override + public boolean isSubselectFetchingEnabled() { + return this.lazySession.get().isSubselectFetchingEnabled(); + } + + @Override + public void setSubselectFetchingEnabled(boolean enabled) { + this.lazySession.get().setSubselectFetchingEnabled( enabled ); + } + @Override public HibernateCriteriaBuilder getCriteriaBuilder() { return this.lazySession.get().getCriteriaBuilder(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 1f6b7d673a..a19f8d424f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -31,7 +31,6 @@ import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.MappingException; import org.hibernate.Session; -import org.hibernate.SessionBuilder; import org.hibernate.SessionEventListener; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; @@ -1360,18 +1359,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im return this; } - @Override - public SessionBuilder defaultBatchFetchSize(int batchSize) { - defaultBatchFetchSize = batchSize; - return this; - } - - @Override - public SessionBuilder subselectFetchEnabled(boolean enabled) { - subselectFetchEnabled = enabled; - return null; - } - @Override public SessionBuilderImpl flushMode(FlushMode flushMode) { this.flushMode = flushMode; diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index e25f8f275e..cc9dc085ae 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1939,20 +1939,22 @@ public class SessionImpl loadQueryInfluencers.disableFetchProfile( name ); } - // TODO: unused for now, should we promote to Session? + @Override public void setSubselectFetchingEnabled(boolean enabled) { loadQueryInfluencers.setSubselectFetchEnabled( enabled ); } + @Override public boolean isSubselectFetchingEnabled() { return loadQueryInfluencers.getSubselectFetchEnabled(); } - // TODO: unused for now, should we promote to Session? + @Override public void setFetchBatchSize(int batchSize) { loadQueryInfluencers.setBatchSize( batchSize ); } + @Override public int getFetchBatchSize() { return loadQueryInfluencers.getBatchSize(); } @@ -2065,7 +2067,9 @@ public class SessionImpl } } - private static class SharedSessionBuilderImpl extends SessionFactoryImpl.SessionBuilderImpl implements SharedSessionBuilder, SharedSessionCreationOptions { + private static class SharedSessionBuilderImpl + extends SessionFactoryImpl.SessionBuilderImpl + implements SharedSessionBuilder, SharedSessionCreationOptions { private final SessionImpl session; private boolean shareTransactionContext; @@ -2156,28 +2160,6 @@ public class SessionImpl return this; } - @Override - public SharedSessionBuilderImpl defaultBatchFetchSize(int batchSize) { - super.defaultBatchFetchSize( batchSize ); - return this; - } - - @Override - public SharedSessionBuilderImpl subselectFetchEnabled(boolean enabled) { - super.subselectFetchEnabled( enabled ); - return this; - } - - @Override - public SharedSessionBuilder defaultBatchFetchSize() { - return defaultBatchFetchSize( session.getFetchBatchSize() ); - } - - @Override - public SharedSessionBuilder subselectFetchEnabled() { - return subselectFetchEnabled( session.isSubselectFetchingEnabled() ); - } - @Override public SharedSessionBuilderImpl autoClose() { autoClose( session.autoClose );