Skip to content

Commit 9f91f2d

Browse files
committed
major refactor to horrible instantiation of QueryEngine
1 parent 2a999d6 commit 9f91f2d

38 files changed

+287
-227
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public Database getDatabase() {
287287
}
288288

289289
@Override
290-
public NamedObjectRepository buildNamedQueryRepository(SessionFactoryImplementor sessionFactory) {
290+
public NamedObjectRepository buildNamedQueryRepository() {
291291
throw new UnsupportedOperationException( "#buildNamedQueryRepository should not be called on InFlightMetadataCollector" );
292292
}
293293

hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171

7272
import static org.hibernate.cfg.AvailableSettings.EVENT_LISTENER_PREFIX;
7373
import static org.hibernate.internal.util.StringHelper.splitAtCommas;
74+
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
7475

7576
/**
7677
* Container for configuration data collected during binding the metamodel.
@@ -356,12 +357,12 @@ public java.util.Collection<Table> collectTableMappings() {
356357
}
357358

358359
@Override
359-
public NamedObjectRepository buildNamedQueryRepository(SessionFactoryImplementor sessionFactory) {
360+
public NamedObjectRepository buildNamedQueryRepository() {
360361
return new NamedObjectRepositoryImpl(
361-
CollectionHelper.mapOfSize( namedQueryMap.size() ),
362-
CollectionHelper.mapOfSize( namedNativeQueryMap.size() ),
363-
CollectionHelper.mapOfSize( namedProcedureCallMap.size() ),
364-
CollectionHelper.mapOfSize( sqlResultSetMappingMap.size() )
362+
mapOfSize( namedQueryMap.size() ),
363+
mapOfSize( namedNativeQueryMap.size() ),
364+
mapOfSize( namedProcedureCallMap.size() ),
365+
mapOfSize( sqlResultSetMappingMap.size() )
365366
);
366367
}
367368

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ public DiscriminatorType<?> resolveEmbeddableDiscriminatorType(
259259
}
260260

261261
@Override
262-
public NamedObjectRepository buildNamedQueryRepository(SessionFactoryImplementor sessionFactory) {
263-
return delegate().buildNamedQueryRepository( sessionFactory );
262+
public NamedObjectRepository buildNamedQueryRepository() {
263+
return delegate().buildNamedQueryRepository();
264264
}
265265

266266
@Override

hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface MetadataImplementor extends Metadata {
4646
*/
4747
SqmFunctionRegistry getFunctionRegistry();
4848

49-
NamedObjectRepository buildNamedQueryRepository(SessionFactoryImplementor sessionFactory);
49+
NamedObjectRepository buildNamedQueryRepository();
5050

5151
@Incubating
5252
void orderColumns(boolean forceOrdering);

hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.hibernate.internal.BaselineSessionEventsListenerBuilder;
2626
import org.hibernate.jpa.spi.JpaCompliance;
2727
import org.hibernate.proxy.EntityNotFoundDelegate;
28-
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
2928
import org.hibernate.query.criteria.ValueHandlingMode;
3029
import org.hibernate.query.spi.QueryEngineOptions;
3130
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@@ -242,10 +241,6 @@ default boolean getNativeJdbcParametersIgnored() {
242241

243242
boolean isFailOnPaginationOverCollectionFetchEnabled();
244243

245-
default ImmutableEntityUpdateQueryHandlingMode getImmutableEntityUpdateQueryHandlingMode() {
246-
return ImmutableEntityUpdateQueryHandlingMode.WARNING;
247-
}
248-
249244
/**
250245
* The default catalog to use in generated SQL when a catalog wasn't specified in the mapping,
251246
* neither explicitly nor implicitly (see the concept of implicit catalog in XML mapping).

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public SessionFactoryImpl(
227227
settings = getSettings( options, serviceRegistry );
228228
maskOutSensitiveInformation( settings );
229229
deprecationCheck( settings );
230-
LOG.debugf( "Instantiating SessionFactory with settings: %s", settings);
230+
LOG.debugf( "Instantiating SessionFactory with settings: %s", settings );
231231

232232
sqlStringGenerationContext = createSqlStringGenerationContext( bootMetamodel, options, jdbcServices );
233233

@@ -280,7 +280,7 @@ public SessionFactoryImpl(
280280
// created, then we can split creation of QueryEngine
281281
// and SqmFunctionRegistry, instantiating just the
282282
// registry here, and doing the engine later
283-
queryEngine = QueryEngineImpl.from( this, bootMetamodel );
283+
queryEngine = QueryEngineImpl.from( bootMetamodel, options, this, serviceRegistry, settings, name );
284284

285285
// create runtime metamodels (mapping and JPA)
286286
final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl();

hibernate-core/src/main/java/org/hibernate/query/BindableType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.query;
88

99
import org.hibernate.Incubating;
10-
import org.hibernate.engine.spi.SessionFactoryImplementor;
1110
import org.hibernate.query.sqm.SqmExpressible;
1211

1312
/**
@@ -30,7 +29,7 @@ default boolean isInstance(J value) {
3029
}
3130

3231
/**
33-
* Resolve this parameter type to the corresponding SqmExpressible
32+
* Resolve this parameter type to the corresponding {@link SqmExpressible}
3433
*/
35-
SqmExpressible<J> resolveExpressible(SessionFactoryImplementor sessionFactory);
34+
SqmExpressible<J> resolveExpressible(BindingContext bindingContext);
3635
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.query;
8+
9+
import org.hibernate.Incubating;
10+
import org.hibernate.metamodel.MappingMetamodel;
11+
import org.hibernate.metamodel.model.domain.JpaMetamodel;
12+
import org.hibernate.type.spi.TypeConfiguration;
13+
14+
/**
15+
* A context within which a {@link BindableType} can be resolved
16+
* to an instance of {@link org.hibernate.query.sqm.SqmExpressible}.
17+
*
18+
* @author Gavin King
19+
*
20+
* @since 7
21+
*/
22+
@Incubating
23+
public interface BindingContext {
24+
TypeConfiguration getTypeConfiguration();
25+
JpaMetamodel getJpaMetamodel();
26+
MappingMetamodel getMappingMetamodel();
27+
}

hibernate-core/src/main/java/org/hibernate/query/internal/BindingTypeHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import java.time.ZonedDateTime;
1414
import java.util.Calendar;
1515

16-
import org.hibernate.engine.spi.SessionFactoryImplementor;
1716
import org.hibernate.metamodel.mapping.JdbcMapping;
1817
import org.hibernate.query.BindableType;
18+
import org.hibernate.query.BindingContext;
1919
import org.hibernate.query.sqm.SqmExpressible;
2020
import org.hibernate.type.StandardBasicTypes;
2121
import org.hibernate.type.descriptor.java.JavaTypeHelper;
@@ -39,9 +39,9 @@ private BindingTypeHelper() {
3939
public <T> BindableType<T> resolveTemporalPrecision(
4040
TemporalType precision,
4141
BindableType<T> declaredParameterType,
42-
SessionFactoryImplementor sessionFactory) {
42+
BindingContext bindingContext) {
4343
if ( precision != null ) {
44-
final SqmExpressible<T> sqmExpressible = declaredParameterType.resolveExpressible( sessionFactory );
44+
final SqmExpressible<T> sqmExpressible = declaredParameterType.resolveExpressible(bindingContext);
4545
if ( !( JavaTypeHelper.isTemporal( sqmExpressible.getExpressibleJavaType() ) ) ) {
4646
throw new UnsupportedOperationException(
4747
"Cannot treat non-temporal parameter type with temporal precision"
@@ -50,7 +50,7 @@ public <T> BindableType<T> resolveTemporalPrecision(
5050

5151
final TemporalJavaType<T> temporalJtd = (TemporalJavaType<T>) sqmExpressible.getExpressibleJavaType();
5252
if ( temporalJtd.getPrecision() != precision ) {
53-
final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
53+
final TypeConfiguration typeConfiguration = bindingContext.getTypeConfiguration();
5454
final TemporalJavaType<T> temporalTypeForPrecision;
5555
// Special case java.util.Date, because TemporalJavaType#resolveTypeForPrecision doesn't support widening,
5656
// since the main purpose of that method is to determine the final java type based on the reflective type

hibernate-core/src/main/java/org/hibernate/query/internal/QueryEngineImpl.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import org.hibernate.boot.model.FunctionContributor;
1111
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
1212
import org.hibernate.boot.spi.MetadataImplementor;
13-
import org.hibernate.boot.spi.SessionFactoryOptions;
1413
import org.hibernate.cfg.AvailableSettings;
1514
import org.hibernate.dialect.Dialect;
15+
import org.hibernate.engine.jdbc.spi.JdbcServices;
1616
import org.hibernate.engine.query.spi.NativeQueryInterpreter;
17-
import org.hibernate.engine.spi.SessionFactoryImplementor;
1817
import org.hibernate.internal.CoreLogging;
1918
import org.hibernate.internal.util.config.ConfigurationHelper;
19+
import org.hibernate.query.BindingContext;
2020
import org.hibernate.query.hql.HqlTranslator;
2121
import org.hibernate.query.hql.internal.StandardHqlTranslator;
2222
import org.hibernate.query.hql.spi.SqmCreationOptions;
@@ -32,15 +32,14 @@
3232
import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
3333
import org.hibernate.query.sqm.sql.StandardSqmTranslatorFactory;
3434
import org.hibernate.service.ServiceRegistry;
35-
import org.hibernate.stat.spi.StatisticsImplementor;
35+
import org.hibernate.service.spi.ServiceRegistryImplementor;
3636
import org.hibernate.type.spi.TypeConfiguration;
3737
import org.jboss.logging.Logger;
3838

3939
import java.util.ArrayList;
4040
import java.util.Collection;
4141
import java.util.List;
4242
import java.util.Map;
43-
import java.util.function.Supplier;
4443

4544
import static java.util.Comparator.comparingInt;
4645

@@ -54,23 +53,31 @@ public class QueryEngineImpl implements QueryEngine {
5453

5554
private static final Logger LOG_HQL_FUNCTIONS = CoreLogging.logger("org.hibernate.HQL_FUNCTIONS");
5655

57-
public static QueryEngine from(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata) {
58-
final QueryEngineOptions options = sessionFactory.getSessionFactoryOptions();
59-
final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
56+
public static QueryEngineImpl from(
57+
MetadataImplementor metadata,
58+
QueryEngineOptions options,
59+
SqmCreationContext sqmCreationContext,
60+
ServiceRegistryImplementor serviceRegistry,
61+
Map<String,Object> properties,
62+
String name) {
63+
final Dialect dialect = serviceRegistry.requireService( JdbcServices.class ).getDialect();
6064
return new QueryEngineImpl(
61-
sessionFactory,
6265
metadata.getTypeConfiguration(),
63-
resolveHqlTranslator( options, dialect, sessionFactory, new SqmCreationOptionsStandard( options ) ),
66+
resolveHqlTranslator( options, dialect, sqmCreationContext, new SqmCreationOptionsStandard( options ) ),
6467
resolveSqmTranslatorFactory( options, dialect ),
65-
createFunctionRegistry( sessionFactory, metadata, options, dialect ),
66-
metadata.buildNamedQueryRepository( sessionFactory ),
67-
buildInterpretationCache( sessionFactory::getStatistics, sessionFactory.getProperties() ),
68-
sessionFactory.getServiceRegistry().getService(NativeQueryInterpreter.class)
68+
createFunctionRegistry( serviceRegistry, metadata, options, dialect ),
69+
metadata.buildNamedQueryRepository(),
70+
buildInterpretationCache( serviceRegistry, properties ),
71+
serviceRegistry.getService(NativeQueryInterpreter.class),
72+
sqmCreationContext,
73+
options,
74+
options.getUuid(),
75+
name
6976
);
7077
}
7178

7279
private static SqmFunctionRegistry createFunctionRegistry(
73-
SessionFactoryImplementor sessionFactory,
80+
ServiceRegistry serviceRegistry,
7481
MetadataImplementor metadata,
7582
QueryEngineOptions queryEngineOptions,
7683
Dialect dialect) {
@@ -84,21 +91,17 @@ private static SqmFunctionRegistry createFunctionRegistry(
8491
}
8592

8693
//TODO: probably better to turn this back into an anonymous class
87-
final FunctionContributions functionContributions = new QueryEngineImpl.FunctionContributionsImpl(
88-
sessionFactory.getServiceRegistry(),
89-
metadata.getTypeConfiguration(),
90-
sqmFunctionRegistry
91-
);
92-
for ( FunctionContributor contributor : sortedFunctionContributors( sessionFactory.getServiceRegistry() ) ) {
94+
final FunctionContributions functionContributions =
95+
new FunctionContributionsImpl( serviceRegistry, metadata.getTypeConfiguration(), sqmFunctionRegistry );
96+
for ( FunctionContributor contributor : sortedFunctionContributors( serviceRegistry ) ) {
9397
contributor.contributeFunctions( functionContributions );
9498
}
9599

96100
dialect.initializeFunctionRegistry( functionContributions );
97101

98102
if ( LOG_HQL_FUNCTIONS.isDebugEnabled() ) {
99-
sqmFunctionRegistry.getFunctionsByName().forEach(
100-
entry -> LOG_HQL_FUNCTIONS.debug( entry.getValue().getSignature( entry.getKey() ) )
101-
);
103+
sqmFunctionRegistry.getFunctionsByName()
104+
.forEach( entry -> LOG_HQL_FUNCTIONS.debug( entry.getValue().getSignature( entry.getKey() ) ) );
102105
}
103106

104107
return sqmFunctionRegistry;
@@ -114,30 +117,30 @@ private static SqmFunctionRegistry createFunctionRegistry(
114117
private final SqmFunctionRegistry sqmFunctionRegistry;
115118

116119
private QueryEngineImpl(
117-
SessionFactoryImplementor sessionFactory,
118120
TypeConfiguration typeConfiguration,
119121
HqlTranslator hqlTranslator,
120122
SqmTranslatorFactory sqmTranslatorFactory,
121123
SqmFunctionRegistry functionRegistry,
122124
NamedObjectRepository namedObjectRepository,
123125
QueryInterpretationCache interpretationCache,
124-
NativeQueryInterpreter nativeQueryInterpreter) {
126+
NativeQueryInterpreter nativeQueryInterpreter,
127+
BindingContext context,
128+
QueryEngineOptions options,
129+
String uuid, String name) {
125130
this.typeConfiguration = typeConfiguration;
126131
this.sqmFunctionRegistry = functionRegistry;
127132
this.sqmTranslatorFactory = sqmTranslatorFactory;
128133
this.hqlTranslator = hqlTranslator;
129134
this.namedObjectRepository = namedObjectRepository;
130135
this.interpretationCache = interpretationCache;
131136
this.nativeQueryInterpreter = nativeQueryInterpreter;
132-
final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
133-
this.criteriaBuilder = new SqmCriteriaNodeBuilder(
134-
sessionFactory.getUuid(),
135-
sessionFactory.getName(),
136-
this,
137-
sessionFactoryOptions.getJpaCompliance().isJpaQueryComplianceEnabled(),
138-
sessionFactoryOptions.getCriteriaValueHandlingMode(),
139-
() -> sessionFactory
140-
);
137+
this.criteriaBuilder = createCriteriaBuilder( context, options, uuid, name );
138+
}
139+
140+
private SqmCriteriaNodeBuilder createCriteriaBuilder(
141+
BindingContext context, QueryEngineOptions options,
142+
String uuid, String name) {
143+
return new SqmCriteriaNodeBuilder( uuid, name, this, options, context );
141144
}
142145

143146
private static HqlTranslator resolveHqlTranslator(
@@ -171,10 +174,10 @@ else if ( dialect.getSqmTranslatorFactory() != null ) {
171174
}
172175

173176
private static List<FunctionContributor> sortedFunctionContributors(ServiceRegistry serviceRegistry) {
174-
Collection<FunctionContributor> functionContributors =
177+
final Collection<FunctionContributor> functionContributors =
175178
serviceRegistry.requireService(ClassLoaderService.class)
176179
.loadJavaServices(FunctionContributor.class);
177-
List<FunctionContributor> contributors = new ArrayList<>( functionContributors );
180+
final List<FunctionContributor> contributors = new ArrayList<>( functionContributors );
178181
contributors.sort(
179182
comparingInt( FunctionContributor::ordinal )
180183
.thenComparing( a -> a.getClass().getCanonicalName() )
@@ -183,8 +186,7 @@ private static List<FunctionContributor> sortedFunctionContributors(ServiceRegis
183186
}
184187

185188
private static QueryInterpretationCache buildInterpretationCache(
186-
Supplier<StatisticsImplementor> statisticsSupplier,
187-
Map<String, Object> properties) {
189+
ServiceRegistry serviceRegistry, Map<String, Object> properties) {
188190
final boolean explicitUseCache = ConfigurationHelper.getBoolean(
189191
AvailableSettings.QUERY_PLAN_CACHE_ENABLED,
190192
properties,
@@ -202,11 +204,11 @@ private static QueryInterpretationCache buildInterpretationCache(
202204
? explicitMaxPlanSize
203205
: QueryEngine.DEFAULT_QUERY_PLAN_MAX_COUNT;
204206

205-
return new QueryInterpretationCacheStandardImpl( size, statisticsSupplier );
207+
return new QueryInterpretationCacheStandardImpl( size, serviceRegistry );
206208
}
207209
else {
208210
// disabled
209-
return new QueryInterpretationCacheDisabledImpl( statisticsSupplier );
211+
return new QueryInterpretationCacheDisabledImpl( serviceRegistry );
210212
}
211213
}
212214

@@ -299,5 +301,4 @@ public ServiceRegistry getServiceRegistry() {
299301
return serviceRegistry;
300302
}
301303
}
302-
303304
}

0 commit comments

Comments
 (0)