From 6f798598a2eb46872b9a3d5e8d2bcda5cdf4215a Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 23 Mar 2018 13:41:09 +0000 Subject: [PATCH] HHH-12444 - Introduce BootstrapContext HHH-12443 - Introduce TypeConfiguration --- .../boot/spi/MetadataImplementor.java | 5 +++ .../internal/SessionFactoryImpl.java | 19 +++++------ .../metamodel/internal/MetamodelImpl.java | 11 ++++++- .../metamodel/spi/MetamodelImplementor.java | 8 +++++ .../hibernate/type/spi/TypeConfiguration.java | 33 ++++++++++++++++--- .../jpa/test/metadata/MetadataTest.java | 2 +- 6 files changed, 61 insertions(+), 17 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java index 85d8b59dd6..0bd1a400c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java @@ -37,6 +37,11 @@ public interface MetadataImplementor extends Metadata, Mapping { */ MetadataBuildingOptions getMetadataBuildingOptions(); + /** + * Access to the TypeConfiguration + * + * @return Access to the TypeConfiguration + */ TypeConfiguration getTypeConfiguration(); /** 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 1f900f4c9b..91a27a48a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -125,7 +125,6 @@ import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; import org.hibernate.type.SerializableType; import org.hibernate.type.Type; import org.hibernate.type.TypeResolver; -import org.hibernate.type.spi.TypeConfiguration; import org.jboss.logging.Logger; @@ -173,7 +172,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { // todo : org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor too? - private final transient MetamodelImpl metamodel; + private final transient MetamodelImplementor metamodel; private final transient CriteriaBuilderImpl criteriaBuilder; private final PersistenceUnitUtil jpaPersistenceUnitUtil; private final transient CacheImplementor cacheAccess; @@ -189,7 +188,6 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { private final transient Map filters; private final transient Map fetchProfiles; - private final transient TypeResolver typeResolver; private final transient TypeHelper typeHelper; public SessionFactoryImpl( @@ -245,10 +243,8 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { for ( SessionFactoryObserver sessionFactoryObserver : options.getSessionFactoryObservers() ) { this.observer.addObserver( sessionFactoryObserver ); } - TypeConfiguration typeConfiguration = metadata.getTypeConfiguration(); - typeConfiguration.scope( this ); - this.typeResolver = typeConfiguration.getTypeResolver(); - this.typeHelper = new TypeLocatorImpl( typeResolver ); + + this.typeHelper = new TypeLocatorImpl( metadata.getTypeConfiguration().getTypeResolver() ); this.filters = new HashMap<>(); this.filters.putAll( metadata.getFilterDefinitions() ); @@ -295,8 +291,11 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { LOG.debug( "Instantiated session factory" ); - this.metamodel = new MetamodelImpl( this ); - this.metamodel.initialize( metadata, determineJpaMetaModelPopulationSetting( properties ) ); + this.metamodel = metadata.getTypeConfiguration().scope( this , bootstrapContext); + ( (MetamodelImpl) this.metamodel ).initialize( + metadata, + determineJpaMetaModelPopulationSetting( properties ) + ); //Named Queries: this.namedQueryRepository = metadata.buildNamedQueryRepository( this ); @@ -563,7 +562,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { */ @Deprecated public TypeResolver getTypeResolver() { - return typeResolver; + return metamodel.getTypeConfiguration().getTypeResolver(); } public QueryPlanCache getQueryPlanCache() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetamodelImpl.java index ef3a4ca082..43eaaaeb2f 100755 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetamodelImpl.java @@ -65,6 +65,7 @@ import org.hibernate.persister.spi.PersisterFactory; import org.hibernate.tuple.entity.EntityTuplizer; import org.hibernate.type.AssociationType; import org.hibernate.type.Type; +import org.hibernate.type.spi.TypeConfiguration; /** * Hibernate implementation of the JPA {@link javax.persistence.metamodel.Metamodel} contract. @@ -94,8 +95,11 @@ public class MetamodelImpl implements MetamodelImplementor, Serializable { private final transient Map entityGraphMap = new ConcurrentHashMap<>(); - public MetamodelImpl(SessionFactoryImplementor sessionFactory) { + private final TypeConfiguration typeConfiguration; + + public MetamodelImpl(SessionFactoryImplementor sessionFactory, TypeConfiguration typeConfiguration) { this.sessionFactory = sessionFactory; + this.typeConfiguration = typeConfiguration; } /** @@ -493,6 +497,11 @@ public class MetamodelImpl implements MetamodelImplementor, Serializable { // this.entityTypesByEntityName = entityTypesByEntityName; // } + @Override + public TypeConfiguration getTypeConfiguration() { + return typeConfiguration; + } + @Override public SessionFactoryImplementor getSessionFactory() { return sessionFactory; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java index 443df5cf6e..2d049d73c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/MetamodelImplementor.java @@ -18,11 +18,19 @@ import org.hibernate.Metamodel; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.spi.TypeConfiguration; /** * @author Steve Ebersole */ public interface MetamodelImplementor extends Metamodel { + /** + * Access to the TypeConfiguration in effect for this SessionFactory/Metamodel + * + * @return Access to the TypeConfiguration + */ + TypeConfiguration getTypeConfiguration(); + @Override SessionFactoryImplementor getSessionFactory(); diff --git a/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java b/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java index 719a1c36ea..2c277367f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java +++ b/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java @@ -7,25 +7,27 @@ package org.hibernate.type.spi; import java.io.Serializable; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.hibernate.HibernateException; import org.hibernate.Incubating; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService; +import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.internal.SessionFactoryRegistry; +import org.hibernate.metamodel.internal.MetamodelImpl; +import org.hibernate.metamodel.spi.MetamodelImplementor; import org.hibernate.type.BasicTypeRegistry; import org.hibernate.type.Type; import org.hibernate.type.TypeFactory; import org.hibernate.type.TypeResolver; -import org.jboss.logging.Logger; - -import static org.hibernate.internal.CoreLogging.logger; import static org.hibernate.internal.CoreLogging.messageLogger; /** @@ -57,6 +59,9 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable { private final BasicTypeRegistry basicTypeRegistry; + private final Map importMap = new ConcurrentHashMap<>(); + + // temporarily needed to support deprecations private final TypeResolver typeResolver; @@ -86,6 +91,10 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable { return basicTypeRegistry; } + public Map getImportMap() { + return Collections.unmodifiableMap( importMap ); + } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Scoping @@ -108,10 +117,24 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable { scope.setMetadataBuildingContext( metadataBuildingContext ); } - public void scope(SessionFactoryImpl sessionFactory) { + public MetamodelImplementor scope(SessionFactoryImplementor sessionFactory, BootstrapContext bootstrapContext) { log.debugf( "Scoping TypeConfiguration [%s] to SessionFactoryImpl [%s]", this, sessionFactory ); scope.setSessionFactory( sessionFactory ); typeFactory.injectSessionFactory( sessionFactory ); + log.debugf( "Scoping TypeConfiguration [%s] to SessionFactory [%s]", this, sessionFactory ); + + for ( Map.Entry importEntry : scope.metadataBuildingContext.getMetadataCollector().getImports().entrySet() ) { + if ( importMap.containsKey( importEntry.getKey() ) ) { + continue; + } + + importMap.put( importEntry.getKey(), importEntry.getValue() ); + } + + scope.setSessionFactory( sessionFactory ); + sessionFactory.addObserver( this ); + MetamodelImpl metamodel = new MetamodelImpl( sessionFactory, this ); + return metamodel; } /** diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/metadata/MetadataTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/metadata/MetadataTest.java index 908c5aead7..a2831032c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/metadata/MetadataTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/metadata/MetadataTest.java @@ -94,7 +94,7 @@ public class MetadataTest extends BaseEntityManagerFunctionalTestCase { .addAnnotatedClass( WithGenericCollection.class ) .buildMetadata(); SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory(); - MetamodelImpl metamodel = new MetamodelImpl( sfi ); + MetamodelImpl metamodel = new MetamodelImpl( sfi, ( (MetadataImplementor) metadata ).getTypeConfiguration() ); metamodel.initialize( (MetadataImplementor) metadata, JpaMetaModelPopulationSetting.IGNORE_UNSUPPORTED ); sfi.close(); }