diff --git a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/NativeQueryInterpreter.java b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/NativeQueryInterpreter.java index d894e1bda1..0c001bcc6a 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/NativeQueryInterpreter.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/NativeQueryInterpreter.java @@ -8,6 +8,8 @@ package org.hibernate.engine.query.spi; import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.loader.custom.CustomLoader; +import org.hibernate.loader.custom.CustomQuery; import org.hibernate.query.internal.ParameterMetadataImpl; import org.hibernate.service.Service; @@ -16,6 +18,7 @@ import org.hibernate.service.Service; * * @author Steve Ebersole * @author Gunnar Morling + * @author Guillaume Smet */ public interface NativeQueryInterpreter extends Service { /** @@ -38,4 +41,17 @@ public interface NativeQueryInterpreter extends Service { * @return A query plan for the specified native query. */ NativeSQLQueryPlan createQueryPlan(NativeSQLQuerySpecification specification, SessionFactoryImplementor sessionFactory); + + /** + * Creates a {@link CustomLoader} for the given {@link CustomQuery}. + * + * @param customQuery The CustomQuery to create a loader for + * @param sessionFactory The current session factory + * + * @deprecated This method will be removed in 6. + */ + @Deprecated + default CustomLoader createCustomLoader(CustomQuery customQuery, SessionFactoryImplementor sessionFactory) { + return new CustomLoader( customQuery, sessionFactory ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/QueryPlanCache.java b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/QueryPlanCache.java index 1ab7b081fb..f3356a3bf6 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/QueryPlanCache.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/QueryPlanCache.java @@ -67,7 +67,7 @@ public class QueryPlanCache implements Serializable { private final BoundedConcurrentHashMap parameterMetadataCache; - private NativeQueryInterpreter nativeQueryInterpreterService; + private NativeQueryInterpreter nativeQueryInterpreter; /** * Constructs the QueryPlanCache to be used by the given SessionFactory @@ -108,7 +108,7 @@ public class QueryPlanCache implements Serializable { BoundedConcurrentHashMap.Eviction.LIRS ); - nativeQueryInterpreterService = factory.getServiceRegistry().getService( NativeQueryInterpreter.class ); + nativeQueryInterpreter = factory.getServiceRegistry().getService( NativeQueryInterpreter.class ); } /** @@ -125,7 +125,7 @@ public class QueryPlanCache implements Serializable { final ParameterMetadataKey key = new ParameterMetadataKey( query, isOrdinalParameterZeroBased ); ParameterMetadataImpl value = parameterMetadataCache.get( key ); if ( value == null ) { - value = nativeQueryInterpreterService.getParameterMetadata( query ); + value = nativeQueryInterpreter.getParameterMetadata( query ); parameterMetadataCache.putIfAbsent( key, value ); } return value; @@ -210,7 +210,7 @@ public class QueryPlanCache implements Serializable { NativeSQLQueryPlan value = (NativeSQLQueryPlan) queryPlanCache.get( spec ); if ( value == null ) { LOG.tracev( "Unable to locate native-sql query plan in cache; generating ({0})", spec.getQueryString() ); - value = nativeQueryInterpreterService.createQueryPlan( spec, factory ); + value = nativeQueryInterpreter.createQueryPlan( spec, factory ); queryPlanCache.putIfAbsent( spec, value ); } else { @@ -228,6 +228,10 @@ public class QueryPlanCache implements Serializable { parameterMetadataCache.clear(); } + public NativeQueryInterpreter getNativeQueryInterpreter() { + return nativeQueryInterpreter; + } + private static class ParameterMetadataKey implements Serializable { private final String query; private final boolean isOrdinalParameterZeroBased; 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 6aaf7adbb4..0bd0730beb 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -422,7 +422,7 @@ public final class SessionImpl else { super.close(); } - + if ( getFactory().getStatistics().isStatisticsEnabled() ) { getFactory().getStatistics().closeSession(); } @@ -1571,12 +1571,12 @@ public final class SessionImpl public ScrollableResultsImplementor scroll(String query, QueryParameters queryParameters) throws HibernateException { checkOpenOrWaitingForAutoClose(); checkTransactionSynchStatus(); - + HQLQueryPlan plan = queryParameters.getQueryPlan(); if ( plan == null ) { plan = getQueryPlan( query, false ); } - + autoFlushIfRequired( plan.getQuerySpaces() ); dontFlushFromFind++; @@ -2128,7 +2128,7 @@ public final class SessionImpl log.tracev( "Scroll SQL query: {0}", customQuery.getSQL() ); } - CustomLoader loader = new CustomLoader( customQuery, getFactory() ); + CustomLoader loader = getFactory().getQueryPlanCache().getNativeQueryInterpreter().createCustomLoader( customQuery, getFactory() ); autoFlushIfRequired( loader.getQuerySpaces() ); @@ -2152,7 +2152,7 @@ public final class SessionImpl log.tracev( "SQL query: {0}", customQuery.getSQL() ); } - CustomLoader loader = new CustomLoader( customQuery, getFactory() ); + CustomLoader loader = getFactory().getQueryPlanCache().getNativeQueryInterpreter().createCustomLoader( customQuery, getFactory() ); autoFlushIfRequired( loader.getQuerySpaces() ); @@ -2467,7 +2467,7 @@ public final class SessionImpl private LobCreator lobCreator() { // Always use NonContextualLobCreator. If ContextualLobCreator is - // used both here and in WrapperOptions, + // used both here and in WrapperOptions, return NonContextualLobCreator.INSTANCE; }