|
25 | 25 | import javax.naming.Reference; |
26 | 26 | import javax.naming.StringRefAddr; |
27 | 27 |
|
28 | | -import org.hibernate.AssertionFailure; |
29 | 28 | import org.hibernate.CustomEntityDirtinessStrategy; |
30 | 29 | import org.hibernate.EntityNameResolver; |
31 | 30 | import org.hibernate.FlushMode; |
|
90 | 89 | import org.hibernate.metamodel.spi.MappingMetamodelImplementor; |
91 | 90 | import org.hibernate.metamodel.spi.RuntimeMetamodelsImplementor; |
92 | 91 | import org.hibernate.metamodel.spi.RuntimeModelCreationContext; |
93 | | -import org.hibernate.procedure.spi.ProcedureCallImplementor; |
94 | 92 | import org.hibernate.proxy.EntityNotFoundDelegate; |
95 | 93 | import org.hibernate.proxy.LazyInitializer; |
96 | | -import org.hibernate.query.BindingContext; |
97 | | -import org.hibernate.query.hql.spi.SqmQueryImplementor; |
98 | 94 | import org.hibernate.query.internal.QueryEngineImpl; |
99 | | -import org.hibernate.query.named.NamedObjectRepository; |
100 | 95 | import org.hibernate.query.spi.QueryEngine; |
101 | | -import org.hibernate.query.spi.QueryImplementor; |
102 | 96 | import org.hibernate.query.sql.internal.SqlTranslationEngineImpl; |
103 | | -import org.hibernate.query.sql.spi.NativeQueryImplementor; |
104 | 97 | import org.hibernate.query.sql.spi.SqlTranslationEngine; |
105 | 98 | import org.hibernate.query.sqm.NodeBuilder; |
106 | 99 | import org.hibernate.query.sqm.function.SqmFunctionRegistry; |
|
167 | 160 | * @author Steve Ebersole |
168 | 161 | * @author Chris Cranford |
169 | 162 | */ |
170 | | -public class SessionFactoryImpl implements SessionFactoryImplementor, BindingContext { |
| 163 | +public class SessionFactoryImpl implements SessionFactoryImplementor { |
171 | 164 | private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class ); |
172 | 165 |
|
173 | 166 | private final String name; |
@@ -290,21 +283,26 @@ public SessionFactoryImpl( |
290 | 283 |
|
291 | 284 | primeSecondLevelCacheRegions( bootMetamodel ); |
292 | 285 |
|
293 | | - // we build this before creating the runtime metamodel |
| 286 | + // create the empty runtime metamodels object |
| 287 | + final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl( typeConfiguration ); |
| 288 | + runtimeMetamodels = runtimeMetamodelsImpl; |
| 289 | + |
| 290 | + // we build this before creating the runtime metamodels |
294 | 291 | // because the persisters need the SqmFunctionRegistry |
295 | | - // to translate SQL formulas ... but, if we fix Dialect |
| 292 | + // to translate SQL formulas. But, if we fix Dialect |
296 | 293 | // as I proposed, so that it can contribute functions |
297 | 294 | // to the SqmFunctionRegistry before the QueryEngine is |
298 | 295 | // created, then we can split creation of QueryEngine |
299 | 296 | // and SqmFunctionRegistry, instantiating just the |
300 | | - // registry here, and doing the engine later |
301 | | - queryEngine = new QueryEngineImpl( bootMetamodel, options, this, serviceRegistry, settings, name ); |
| 297 | + // registry here, and doing the engine later, and we |
| 298 | + // can thus untie this nasty little knot. Alternatively, |
| 299 | + // perhaps it's not really appropriate that they use the |
| 300 | + // SqmFunctionRegistry for that purpose at all? |
| 301 | + queryEngine = new QueryEngineImpl( bootMetamodel, options, runtimeMetamodels, serviceRegistry, settings, name ); |
302 | 302 | final Map<String, FetchProfile> fetchProfiles = new HashMap<>(); |
303 | 303 | sqlTranslationEngine = new SqlTranslationEngineImpl( this, typeConfiguration, fetchProfiles ); |
304 | 304 |
|
305 | | - // create runtime metamodels (mapping and JPA) |
306 | | - final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl(); |
307 | | - runtimeMetamodels = runtimeMetamodelsImpl; |
| 305 | + // now actually create the mapping and JPA metamodels |
308 | 306 | final MappingMetamodelImpl mappingMetamodelImpl = new MappingMetamodelImpl( typeConfiguration, serviceRegistry ); |
309 | 307 | runtimeMetamodelsImpl.setMappingMetamodel( mappingMetamodelImpl ); |
310 | 308 | mappingMetamodelImpl.finishInitialization( |
@@ -478,10 +476,12 @@ private SessionBuilderImpl buildTemporarySessionOpenOptions() { |
478 | 476 | private void primeSecondLevelCacheRegions(MetadataImplementor mappingMetadata) { |
479 | 477 | final Map<String, DomainDataRegionConfigImpl.Builder> regionConfigBuilders = new ConcurrentHashMap<>(); |
480 | 478 |
|
481 | | - // todo : ultimately this code can be made more efficient when we have a better intrinsic understanding of the hierarchy as a whole |
| 479 | + // TODO: ultimately this code can be made more efficient when we have |
| 480 | + // a better intrinsic understanding of the hierarchy as a whole |
482 | 481 |
|
483 | 482 | for ( PersistentClass bootEntityDescriptor : mappingMetadata.getEntityBindings() ) { |
484 | | - final AccessType accessType = AccessType.fromExternalName( bootEntityDescriptor.getCacheConcurrencyStrategy() ); |
| 483 | + final AccessType accessType = |
| 484 | + AccessType.fromExternalName( bootEntityDescriptor.getCacheConcurrencyStrategy() ); |
485 | 485 |
|
486 | 486 | if ( accessType != null ) { |
487 | 487 | if ( bootEntityDescriptor.isCached() ) { |
@@ -530,29 +530,25 @@ private void primeSecondLevelCacheRegions(MetadataImplementor mappingMetadata) { |
530 | 530 | } |
531 | 531 |
|
532 | 532 | @Override |
533 | | - public SessionImplementor openSession() throws HibernateException { |
534 | | - //The defaultSessionOpenOptions can't be used in some cases; for example when using a TenantIdentifierResolver. |
535 | | - if ( defaultSessionOpenOptions != null ) { |
536 | | - return defaultSessionOpenOptions.openSession(); |
537 | | - } |
538 | | - else { |
539 | | - return withOptions().openSession(); |
540 | | - } |
| 533 | + public SessionImplementor openSession() { |
| 534 | + // The defaultSessionOpenOptions can't be used in some cases; |
| 535 | + // for example when using a TenantIdentifierResolver. |
| 536 | + return defaultSessionOpenOptions != null |
| 537 | + ? defaultSessionOpenOptions.openSession() |
| 538 | + : withOptions().openSession(); |
541 | 539 | } |
542 | 540 |
|
543 | 541 | @Override |
544 | | - public SessionImpl openTemporarySession() throws HibernateException { |
545 | | - //The temporarySessionOpenOptions can't be used in some cases; for example when using a TenantIdentifierResolver. |
546 | | - if ( temporarySessionOpenOptions != null ) { |
547 | | - return temporarySessionOpenOptions.openSession(); |
548 | | - } |
549 | | - else { |
550 | | - return buildTemporarySessionOpenOptions().openSession(); |
551 | | - } |
| 542 | + public SessionImpl openTemporarySession() { |
| 543 | + // The temporarySessionOpenOptions can't be used in some cases; |
| 544 | + // for example when using a TenantIdentifierResolver. |
| 545 | + return temporarySessionOpenOptions != null |
| 546 | + ? temporarySessionOpenOptions.openSession() |
| 547 | + : buildTemporarySessionOpenOptions().openSession(); |
552 | 548 | } |
553 | 549 |
|
554 | 550 | @Override |
555 | | - public Session getCurrentSession() throws HibernateException { |
| 551 | + public Session getCurrentSession() { |
556 | 552 | if ( currentSessionContext == null ) { |
557 | 553 | throw new HibernateException( "No CurrentSessionContext configured" ); |
558 | 554 | } |
@@ -791,7 +787,7 @@ public Reference getReference() { |
791 | 787 | * collector release the memory. |
792 | 788 | */ |
793 | 789 | @Override |
794 | | - public void close() throws HibernateException { |
| 790 | + public void close() { |
795 | 791 | synchronized (this) { |
796 | 792 | if ( status != Status.OPEN ) { |
797 | 793 | if ( getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) { |
@@ -871,58 +867,7 @@ public PersistenceUnitTransactionType getTransactionType() { |
871 | 867 | @Override |
872 | 868 | public void addNamedQuery(String name, Query query) { |
873 | 869 | validateNotClosed(); |
874 | | - |
875 | | - // NOTE : we use Query#unwrap here (rather than direct type checking) |
876 | | - // to account for possibly wrapped query implementations |
877 | | - |
878 | | - // first, handle StoredProcedureQuery |
879 | | - final NamedObjectRepository namedObjectRepository = getQueryEngine().getNamedObjectRepository(); |
880 | | - try { |
881 | | - final ProcedureCallImplementor<?> unwrapped = query.unwrap( ProcedureCallImplementor.class ); |
882 | | - if ( unwrapped != null ) { |
883 | | - namedObjectRepository.registerCallableQueryMemento( name, unwrapped.toMemento( name ) ); |
884 | | - return; |
885 | | - } |
886 | | - } |
887 | | - catch ( PersistenceException ignore ) { |
888 | | - // this means 'query' is not a ProcedureCallImplementor |
889 | | - } |
890 | | - |
891 | | - // then try as a native-SQL or JPQL query |
892 | | - try { |
893 | | - final QueryImplementor<?> queryImplementor = query.unwrap( QueryImplementor.class ); |
894 | | - if ( queryImplementor != null ) { |
895 | | - // create and register the proper NamedQueryDefinition... |
896 | | - if ( queryImplementor instanceof NativeQueryImplementor<?> nativeQueryImplementor ) { |
897 | | - namedObjectRepository.registerNativeQueryMemento( |
898 | | - name, |
899 | | - nativeQueryImplementor.toMemento( name ) |
900 | | - ); |
901 | | - |
902 | | - } |
903 | | - else if ( queryImplementor instanceof SqmQueryImplementor<?> sqmQueryImplementor ) { |
904 | | - namedObjectRepository.registerSqmQueryMemento( |
905 | | - name, |
906 | | - sqmQueryImplementor.toMemento( name ) |
907 | | - ); |
908 | | - } |
909 | | - else { |
910 | | - throw new AssertionFailure("unknown QueryImplementor"); |
911 | | - } |
912 | | - return; |
913 | | - } |
914 | | - } |
915 | | - catch ( PersistenceException ignore ) { |
916 | | - // this means 'query' is not a native-SQL or JPQL query |
917 | | - } |
918 | | - |
919 | | - // if we get here, we are unsure how to properly unwrap the incoming query to extract the needed information |
920 | | - throw new PersistenceException( |
921 | | - String.format( |
922 | | - "Unsure how to properly unwrap given Query [%s] as basis for named query", |
923 | | - query |
924 | | - ) |
925 | | - ); |
| 870 | + getQueryEngine().getNamedObjectRepository().registerNamedQuery( name, query ); |
926 | 871 | } |
927 | 872 |
|
928 | 873 | @Override |
@@ -1001,7 +946,7 @@ public StatisticsImplementor getStatistics() { |
1001 | 946 | return statistics; |
1002 | 947 | } |
1003 | 948 |
|
1004 | | - public FilterDefinition getFilterDefinition(String filterName) throws HibernateException { |
| 949 | + public FilterDefinition getFilterDefinition(String filterName) { |
1005 | 950 | final FilterDefinition filterDefinition = filters.get( filterName ); |
1006 | 951 | if ( filterDefinition == null ) { |
1007 | 952 | throw new UnknownFilterException( filterName ); |
|
0 commit comments