From 10cdb47a97bdde774dbc87b04cfc12f407c59e1d Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 2 Dec 2019 15:05:26 -0600 Subject: [PATCH] HHH-13763 : Update all load-by-key handling to use SQL AST * Cleanup * Prep for dropping LoadPlan --- .../SingleIdEntityLoaderDynamicBatch.java | 9 ++++++++ ...SingleIdEntityLoaderProvidedQueryImpl.java | 12 ++++++++++ .../SingleIdEntityLoaderStandardImpl.java | 17 +++++++++++++- .../loader/ast/internal/SingleIdLoadPlan.java | 23 ++++++++++++++++--- .../loader/ast/spi/SingleIdEntityLoader.java | 6 +++++ .../entity/AbstractEntityPersister.java | 6 ++--- .../JdbcSelectExecutorStandardImpl.java | 7 +++--- .../sql/exec/spi/ExecutionContext.java | 7 ++++++ .../JdbcValuesSourceProcessingOptions.java | 4 +--- 9 files changed, 77 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderDynamicBatch.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderDynamicBatch.java index 4b938a69dd..9ce278d8f4 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderDynamicBatch.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderDynamicBatch.java @@ -179,4 +179,13 @@ public class SingleIdEntityLoaderDynamicBatch extends SingleIdEntityLoaderSup //noinspection unchecked return (T) session.getPersistenceContext().getEntity( entityKey ); } + + @Override + public T load( + Object pkValue, + Object entityInstance, + LockOptions lockOptions, + SharedSessionContractImplementor session) { + return singleIdLoader.load( pkValue, entityInstance, lockOptions, session ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderProvidedQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderProvidedQueryImpl.java index bc5224a6f8..3dde4b38a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderProvidedQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderProvidedQueryImpl.java @@ -69,6 +69,18 @@ public class SingleIdEntityLoaderProvidedQueryImpl implements SingleIdEntityL return query.uniqueResult(); } + @Override + public T load( + Object pkValue, + Object entityInstance, + LockOptions lockOptions, + SharedSessionContractImplementor session) { + if ( entityInstance != null ) { + throw new UnsupportedOperationException( ); + } + return load( pkValue, lockOptions, session ); + } + @Override public Object[] loadDatabaseSnapshot(Object id, SharedSessionContractImplementor session) { return new Object[0]; diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java index 1f3c58b0f4..7865ac5f23 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java @@ -59,7 +59,22 @@ public class SingleIdEntityLoaderStandardImpl extends SingleIdEntityLoaderSup session.getFactory() ); - return loadPlan.load( key, lockOptions, session ); + return loadPlan.load( key, lockOptions, null, session ); + } + + @Override + public T load( + Object key, + Object entityInstance, + LockOptions lockOptions, + SharedSessionContractImplementor session) { + final SingleIdLoadPlan loadPlan = resolveLoadPlan( + lockOptions, + session.getLoadQueryInfluencers(), + session.getFactory() + ); + + return loadPlan.load( key, lockOptions, entityInstance, session ); } @Internal diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java index a844d2606f..19d589f642 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java @@ -21,12 +21,12 @@ import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.SqlAstTranslatorFactory; +import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl; import org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl; import org.hibernate.sql.exec.spi.Callback; import org.hibernate.sql.exec.spi.ExecutionContext; -import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.exec.spi.JdbcParameterBinding; import org.hibernate.sql.exec.spi.JdbcParameterBindings; import org.hibernate.sql.exec.spi.JdbcSelect; @@ -70,7 +70,18 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { return sqlAst; } - T load(Object restrictedValue, LockOptions lockOptions, SharedSessionContractImplementor session) { + T load( + Object restrictedValue, + LockOptions lockOptions, + SharedSessionContractImplementor session) { + return load( restrictedValue, lockOptions, null, session ); + } + + T load( + Object restrictedValue, + LockOptions lockOptions, + Object entityInstance, + SharedSessionContractImplementor session) { final SessionFactoryImplementor sessionFactory = session.getFactory(); final JdbcServices jdbcServices = sessionFactory.getJdbcServices(); final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment(); @@ -118,6 +129,11 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { return session; } + @Override + public Object getEntityInstance() { + return entityInstance; + } + @Override public QueryOptions getQueryOptions() { return QueryOptions.NONE; @@ -130,7 +146,8 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { @Override public Callback getCallback() { - return null; + return afterLoadAction -> { + }; } }, RowTransformerPassThruImpl.instance() diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/spi/SingleIdEntityLoader.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/spi/SingleIdEntityLoader.java index 1f1823dd4d..f120d4248c 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/spi/SingleIdEntityLoader.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/spi/SingleIdEntityLoader.java @@ -21,6 +21,12 @@ public interface SingleIdEntityLoader extends SingleEntityLoader { @Override T load(Object pkValue, LockOptions lockOptions, SharedSessionContractImplementor session); + /** + * Load by primary key value, populating the passed entity instance. Used to initialize an uninitialized + * bytecode-proxy. The passed instance is the enhanced proxy + */ + T load(Object pkValue, Object entityInstance, LockOptions lockOptions, SharedSessionContractImplementor session); + /** * Load database snapshot by primary key value */ diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 2f20312603..5db3ac8800 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -4616,11 +4616,11 @@ public abstract class AbstractEntityPersister final EntityKey entityKey = proxyInterceptor.getEntityKey(); final Serializable identifier = entityKey.getIdentifier(); - final Object loaded = readLockLoader.load( + final Object loaded = singleIdEntityLoader.load( identifier, entity, - session, - LockOptions.READ + LockOptions.READ, + session ); if ( loaded == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java index 11e5bd46e8..35df3b6bb2 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java @@ -6,7 +6,6 @@ */ package org.hibernate.sql.exec.internal; -import java.io.Serializable; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; @@ -129,7 +128,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { ExecutionContext executionContext, RowTransformer rowTransformer, Function statementCreator, - ResultsConsumer resultsConsumer) { + ResultsConsumer resultsConsumer) { final JdbcValues jdbcValues = resolveJdbcValuesSource( jdbcSelect, @@ -148,7 +147,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() { @Override public Object getEffectiveOptionalObject() { - return null; + return executionContext.getEntityInstance(); } @Override @@ -157,7 +156,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { } @Override - public Serializable getEffectiveOptionalId() { + public Object getEffectiveOptionalId() { return null; } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java index a754c0fd02..14593c88c2 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java @@ -38,6 +38,13 @@ public interface ExecutionContext { return null; } + /** + * Should only be used when initializing a bytecode-proxy + */ + default Object getEntityInstance() { + return null; + } + default void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) { // by default do nothing } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValuesSourceProcessingOptions.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValuesSourceProcessingOptions.java index 97abcebf3b..b3a1eae809 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValuesSourceProcessingOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValuesSourceProcessingOptions.java @@ -6,8 +6,6 @@ */ package org.hibernate.sql.results.jdbc.spi; -import java.io.Serializable; - /** * Essentially processing options only for entity loading * @@ -16,7 +14,7 @@ import java.io.Serializable; public interface JdbcValuesSourceProcessingOptions { Object getEffectiveOptionalObject(); String getEffectiveOptionalEntityName(); - Serializable getEffectiveOptionalId(); + Object getEffectiveOptionalId(); boolean shouldReturnProxies(); }