diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java index 96912893af..f39b55bf76 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java @@ -56,6 +56,7 @@ import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; import org.hibernate.boot.model.relational.Database; import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.QualifiedTableName; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.boot.model.source.internal.ImplicitColumnNamingSecondPass; import org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext; import org.hibernate.boot.models.categorize.internal.ClassLoaderServiceLoading; @@ -78,6 +79,8 @@ import org.hibernate.boot.spi.PropertyData; import org.hibernate.boot.spi.SecondPass; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreLogging; @@ -120,6 +123,9 @@ import jakarta.persistence.Entity; import jakarta.persistence.MapsId; import static org.hibernate.boot.model.naming.Identifier.toIdentifier; +import static org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl.fromExplicit; +import static org.hibernate.cfg.MappingSettings.DEFAULT_CATALOG; +import static org.hibernate.cfg.MappingSettings.DEFAULT_SCHEMA; import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize; /** @@ -132,7 +138,8 @@ import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize * * @author Steve Ebersole */ -public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, ConverterRegistry { +public class InFlightMetadataCollectorImpl + implements InFlightMetadataCollector, ConverterRegistry, GeneratorSettings { private static final CoreMessageLogger log = CoreLogging.messageLogger( InFlightMetadataCollectorImpl.class ); private final BootstrapContext bootstrapContext; @@ -170,6 +177,8 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, private Map sqlFunctionMap; + final ConfigurationService configurationService; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // All the annotation-processing-specific state :( private final Set defaultIdentifierGeneratorNames = new HashSet<>(); @@ -210,16 +219,12 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, } bootstrapContext.getAuxiliaryDatabaseObjectList().forEach( getDatabase()::addAuxiliaryDatabaseObject ); + + configurationService = bootstrapContext.getServiceRegistry().requireService(ConfigurationService.class); } - public InFlightMetadataCollectorImpl( - BootstrapContext bootstrapContext, - MetadataBuildingOptions options) { - this( - bootstrapContext, - createModelBuildingContext( bootstrapContext ), - options - ); + public InFlightMetadataCollectorImpl(BootstrapContext bootstrapContext, MetadataBuildingOptions options) { + this( bootstrapContext, createModelBuildingContext( bootstrapContext ), options ); } private static SourceModelBuildingContext createModelBuildingContext(BootstrapContext bootstrapContext) { @@ -2204,20 +2209,7 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, private void handleIdentifierValueBinding( KeyValue identifierValueBinding, Dialect dialect, RootClass entityBinding, Property identifierProperty) { try { - identifierValueBinding.createGenerator( dialect, entityBinding, identifierProperty, - new GeneratorSettings() { - @Override - public String getDefaultCatalog() { - //TODO: does not have access to property-configured default - return persistenceUnitMetadata.getDefaultCatalog(); - } - - @Override - public String getDefaultSchema() { - //TODO: does not have access to property-configured default - return persistenceUnitMetadata.getDefaultSchema(); - } - } ); + identifierValueBinding.createGenerator( dialect, entityBinding, identifierProperty, this ); } catch (MappingException e) { // ignore this for now. The reasoning being "non-reflective" binding as needed @@ -2227,4 +2219,21 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, log.debugf( "Ignoring exception thrown when trying to build IdentifierGenerator as part of Metadata building", e ); } } + + @Override + public String getDefaultCatalog() { + final String defaultCatalog = configurationService.getSetting( DEFAULT_CATALOG, StandardConverters.STRING ); + return defaultCatalog == null ? persistenceUnitMetadata.getDefaultCatalog() : defaultCatalog; + } + + @Override + public String getDefaultSchema() { + final String defaultSchema = configurationService.getSetting( DEFAULT_SCHEMA, StandardConverters.STRING ); + return defaultSchema == null ? persistenceUnitMetadata.getDefaultSchema() : defaultSchema; + } + + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + return fromExplicit( database.getJdbcEnvironment(), database, getDefaultCatalog(), getDefaultSchema() ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java index 74f6fa97ac..645f5ee183 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java @@ -244,6 +244,8 @@ public interface SessionFactoryOptions extends QueryEngineOptions { * neither explicitly nor implicitly (see the concept of implicit catalog in XML mapping). * * @return The default catalog to use. + * + * @see org.hibernate.cfg.MappingSettings#DEFAULT_CATALOG */ default String getDefaultCatalog() { return null; @@ -254,6 +256,8 @@ public interface SessionFactoryOptions extends QueryEngineOptions { * neither explicitly nor implicitly (see the concept of implicit schema in XML mapping). * * @return The default schema to use. + * + * @see org.hibernate.cfg.MappingSettings#DEFAULT_SCHEMA */ default String getDefaultSchema() { return null; diff --git a/hibernate-core/src/main/java/org/hibernate/generator/GeneratorCreationContext.java b/hibernate-core/src/main/java/org/hibernate/generator/GeneratorCreationContext.java index c68031ab00..f209f17f0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/generator/GeneratorCreationContext.java +++ b/hibernate-core/src/main/java/org/hibernate/generator/GeneratorCreationContext.java @@ -28,7 +28,9 @@ import static org.hibernate.boot.model.relational.internal.SqlStringGenerationCo @Incubating public interface GeneratorCreationContext { /** - * View of the relational database objects (tables, sequences, ...) and namespaces (catalogs and schemas). + * View of the relational database objects (tables, sequences, etc.) + * and namespaces (catalogs and schemas). Generators may add new + * tables or sequences to the returned {@link Database}. */ Database getDatabase(); @@ -69,6 +71,9 @@ public interface GeneratorCreationContext { return getProperty().getType(); } + /** + * The {@link SqlStringGenerationContext} to use when generating SQL. + */ default SqlStringGenerationContext getSqlStringGenerationContext() { final Database database = getDatabase(); return fromExplicit( database.getJdbcEnvironment(), database, getDefaultCatalog(), getDefaultSchema() ); 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 830875bbba..1a669eb9ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -72,6 +72,7 @@ import org.hibernate.integrator.spi.IntegratorService; import org.hibernate.jpa.internal.ExceptionMapperLegacyJpaImpl; import org.hibernate.jpa.internal.PersistenceUnitUtilImpl; import org.hibernate.mapping.Collection; +import org.hibernate.mapping.GeneratorSettings; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.RootClass; import org.hibernate.metamodel.internal.RuntimeMetamodelsImpl; @@ -277,16 +278,16 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im // create runtime metamodels (mapping and JPA) final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl(); runtimeMetamodels = runtimeMetamodelsImpl; - final MappingMetamodelImpl mappingMetamodelImpl = - new MappingMetamodelImpl( typeConfiguration, serviceRegistry ); + final MappingMetamodelImpl mappingMetamodelImpl = new MappingMetamodelImpl( typeConfiguration, serviceRegistry ); runtimeMetamodelsImpl.setMappingMetamodel( mappingMetamodelImpl ); fastSessionServices = new FastSessionServices( this ); - initializeMappingModel( mappingMetamodelImpl, bootstrapContext, bootMetamodel, options ); + mappingMetamodelImpl.finishInitialization( + new ModelCreationContext( bootstrapContext, bootMetamodel, mappingMetamodelImpl, typeConfiguration ) ); runtimeMetamodelsImpl.setJpaMetamodel( mappingMetamodelImpl.getJpaMetamodel() ); // this needs to happen after the mapping metamodel is // completely built, since we need to use the persisters - fetchProfiles = getFetchProfiles( bootMetamodel, runtimeMetamodels ); + fetchProfiles = getFetchProfiles( bootMetamodel, runtimeMetamodelsImpl); defaultSessionOpenOptions = createDefaultSessionOpenOptionsIfPossible(); temporarySessionOpenOptions = defaultSessionOpenOptions == null ? null : buildTemporarySessionOpenOptions(); @@ -320,102 +321,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im LOG.debug( "Instantiated SessionFactory" ); } - private void initializeMappingModel( - MappingMetamodelImpl mappingMetamodelImpl, - BootstrapContext bootstrapContext, - MetadataImplementor bootMetamodel, - SessionFactoryOptions options) { - mappingMetamodelImpl.finishInitialization( runtimeModelCreationContext( - bootstrapContext, - bootMetamodel, - mappingMetamodelImpl, - mappingMetamodelImpl.getTypeConfiguration(), - options - ) ); - } - - private RuntimeModelCreationContext runtimeModelCreationContext( - BootstrapContext bootstrapContext, - MetadataImplementor bootMetamodel, - MappingMetamodelImplementor mappingMetamodel, - TypeConfiguration typeConfiguration, - SessionFactoryOptions options) { - return new RuntimeModelCreationContext() { - final Map generators = new HashMap<>(); - - @Override - public BootstrapContext getBootstrapContext() { - return bootstrapContext; - } - - @Override - public SessionFactoryImplementor getSessionFactory() { - // this is bad, we're not yet fully-initialized - return SessionFactoryImpl.this; - } - - @Override - public MetadataImplementor getBootModel() { - return bootMetamodel; - } - - @Override - public MappingMetamodelImplementor getDomainModel() { - return mappingMetamodel; - } - - @Override - public CacheImplementor getCache() { - return cacheAccess; - } - - @Override - public Map getSettings() { - return settings; - } - - @Override - public Dialect getDialect() { - return jdbcServices.getDialect(); - } - - @Override - public SqmFunctionRegistry getFunctionRegistry() { - return queryEngine.getSqmFunctionRegistry(); - } - - @Override - public TypeConfiguration getTypeConfiguration() { - return typeConfiguration; - } - - @Override - public SessionFactoryOptions getSessionFactoryOptions() { - return options; - } - - @Override - public JdbcServices getJdbcServices() { - return jdbcServices; - } - - @Override - public SqlStringGenerationContext getSqlStringGenerationContext() { - return sqlStringGenerationContext; - } - - @Override - public ServiceRegistry getServiceRegistry() { - return serviceRegistry; - } - - @Override - public Map getGenerators() { - return generators; - } - }; - } - private static SqlStringGenerationContext createSqlStringGenerationContext( MetadataImplementor bootMetamodel, SessionFactoryOptions options, @@ -1687,4 +1592,110 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im CLOSING, CLOSED } + + private class ModelCreationContext implements RuntimeModelCreationContext, GeneratorSettings { + final Map generators; + private final BootstrapContext bootstrapContext; + private final MetadataImplementor bootMetamodel; + private final MappingMetamodelImplementor mappingMetamodel; + private final TypeConfiguration typeConfiguration; + + private ModelCreationContext( + BootstrapContext bootstrapContext, + MetadataImplementor bootMetamodel, + MappingMetamodelImplementor mappingMetamodel, + TypeConfiguration typeConfiguration) { + this.bootstrapContext = bootstrapContext; + this.bootMetamodel = bootMetamodel; + this.mappingMetamodel = mappingMetamodel; + this.typeConfiguration = typeConfiguration; + generators = new HashMap<>(); + } + + @Override + public BootstrapContext getBootstrapContext() { + return bootstrapContext; + } + + @Override + public SessionFactoryImplementor getSessionFactory() { + // this is bad, we're not yet fully-initialized + return SessionFactoryImpl.this; + } + + @Override + public MetadataImplementor getBootModel() { + return bootMetamodel; + } + + @Override + public MappingMetamodelImplementor getDomainModel() { + return mappingMetamodel; + } + + @Override + public CacheImplementor getCache() { + return cacheAccess; + } + + @Override + public Map getSettings() { + return settings; + } + + @Override + public Dialect getDialect() { + return jdbcServices.getDialect(); + } + + @Override + public SqmFunctionRegistry getFunctionRegistry() { + return queryEngine.getSqmFunctionRegistry(); + } + + @Override + public TypeConfiguration getTypeConfiguration() { + return typeConfiguration; + } + + @Override + public SessionFactoryOptions getSessionFactoryOptions() { + return sessionFactoryOptions; + } + + @Override + public JdbcServices getJdbcServices() { + return jdbcServices; + } + + @Override + public ServiceRegistry getServiceRegistry() { + return serviceRegistry; + } + + @Override + public Map getGenerators() { + return generators; + } + + @Override + public GeneratorSettings getGeneratorSettings() { + return this; + } + + @Override + public String getDefaultCatalog() { + return sessionFactoryOptions.getDefaultCatalog(); + } + + @Override + public String getDefaultSchema() { + return sessionFactoryOptions.getDefaultSchema(); + } + + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + return sqlStringGenerationContext; + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/GeneratorSettings.java b/hibernate-core/src/main/java/org/hibernate/mapping/GeneratorSettings.java index 9b96ee59f1..c5cf5ad7fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/GeneratorSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/GeneratorSettings.java @@ -5,10 +5,29 @@ package org.hibernate.mapping; +import org.hibernate.Incubating; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; +import org.hibernate.dialect.Dialect; + /** + * Exposes the default catalog and schema to the + * {@linkplain KeyValue#createGenerator(Dialect, RootClass, Property, GeneratorSettings) + * generator creation process}. The defaults specified here are ultimately + * passed to the {@linkplain org.hibernate.generator.Generator generator} + * itself via the {@link org.hibernate.generator.GeneratorCreationContext}. + * + * @see org.hibernate.cfg.MappingSettings#DEFAULT_CATALOG + * @see org.hibernate.cfg.MappingSettings#DEFAULT_SCHEMA + * @see org.hibernate.boot.spi.SessionFactoryOptions#getDefaultCatalog() + * @see org.hibernate.boot.spi.SessionFactoryOptions#getDefaultSchema() + * + * @since 7 + * * @author Gavin King */ +@Incubating public interface GeneratorSettings { String getDefaultCatalog(); String getDefaultSchema(); + SqlStringGenerationContext getSqlStringGenerationContext(); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/KeyValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/KeyValue.java index 7650f27397..4118269d9e 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/KeyValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/KeyValue.java @@ -4,6 +4,7 @@ */ package org.hibernate.mapping; +import org.hibernate.Incubating; import org.hibernate.dialect.Dialect; import org.hibernate.generator.Generator; @@ -25,24 +26,9 @@ public interface KeyValue extends Value { boolean isUpdateable(); - @Deprecated(since = "7.0") - default Generator createGenerator(Dialect dialect, RootClass rootClass) { - return createGenerator( dialect, rootClass, null, new GeneratorSettings() { - @Override - public String getDefaultCatalog() { - return null; - } + @Deprecated(since = "7.0", forRemoval = true) + Generator createGenerator(Dialect dialect, RootClass rootClass); - @Override - public String getDefaultSchema() { - return null; - } - } ); - } - - Generator createGenerator( - Dialect dialect, - RootClass rootClass, - Property property, - GeneratorSettings defaults); + @Incubating + Generator createGenerator(Dialect dialect, RootClass rootClass, Property property, GeneratorSettings defaults); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index b46af15ff5..8474cdeebb 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -26,6 +26,7 @@ import org.hibernate.boot.model.convert.spi.ConverterDescriptor; import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext; import org.hibernate.boot.model.internal.AnnotatedJoinColumns; import org.hibernate.boot.model.relational.Database; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.spi.InFlightMetadataCollector; @@ -61,6 +62,7 @@ import static java.lang.Boolean.parseBoolean; import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX; import static org.hibernate.boot.model.internal.GeneratorBinder.ASSIGNED_GENERATOR_NAME; import static org.hibernate.boot.model.internal.GeneratorBinder.ASSIGNED_IDENTIFIER_GENERATOR_CREATOR; +import static org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl.fromExplicit; import static org.hibernate.internal.util.collections.ArrayHelper.toBooleanArray; /** @@ -373,6 +375,28 @@ public abstract class SimpleValue implements KeyValue { return customIdGeneratorCreator; } + @Deprecated(since = "7.0", forRemoval = true) + @Override @SuppressWarnings("removal") + public Generator createGenerator(Dialect dialect, RootClass rootClass) { + return createGenerator( dialect, rootClass, null, new GeneratorSettings() { + @Override + public String getDefaultCatalog() { + return null; + } + + @Override + public String getDefaultSchema() { + return null; + } + + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + final Database database = buildingContext.getMetadataCollector().getDatabase(); + return fromExplicit( database.getJdbcEnvironment(), database, getDefaultCatalog(), getDefaultSchema() ); + } + } ); + } + @Override public Generator createGenerator( Dialect dialect, @@ -857,8 +881,7 @@ public abstract class SimpleValue implements KeyValue { for ( int i = 0; i < columns.size(); i++ ) { final Selectable selectable = columns.get(i); - if ( selectable instanceof Column ) { - final Column column = (Column) selectable; + if ( selectable instanceof Column column ) { columnNames[i] = column.getName(); columnLengths[i] = column.getLength(); } @@ -1054,6 +1077,11 @@ public abstract class SimpleValue implements KeyValue { return buildingContext.getBootstrapContext().getServiceRegistry(); } + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + return defaults.getSqlStringGenerationContext(); + } + @Override public String getDefaultCatalog() { return defaults.getDefaultCatalog(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/RuntimeModelCreationContext.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/RuntimeModelCreationContext.java index 9968b2eb64..198d3f9674 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/RuntimeModelCreationContext.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/RuntimeModelCreationContext.java @@ -13,6 +13,7 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.generator.Generator; +import org.hibernate.mapping.GeneratorSettings; import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.service.ServiceRegistry; import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry; @@ -61,4 +62,6 @@ public interface RuntimeModelCreationContext { ServiceRegistry getServiceRegistry(); Map getGenerators(); + + GeneratorSettings getGeneratorSettings(); } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index 8e08d7e5b9..c9980b003d 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -64,7 +64,6 @@ import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Collection; import org.hibernate.mapping.Column; import org.hibernate.mapping.Formula; -import org.hibernate.mapping.GeneratorSettings; import org.hibernate.mapping.IdentifierCollection; import org.hibernate.mapping.IndexedCollection; import org.hibernate.mapping.PersistentClass; @@ -596,18 +595,7 @@ public abstract class AbstractCollectionPersister private BeforeExecutionGenerator createGenerator(RuntimeModelCreationContext context, IdentifierCollection collection) { final Generator generator = collection.getIdentifier() - .createGenerator( context.getDialect(), null, null, - new GeneratorSettings() { - @Override - public String getDefaultCatalog() { - return context.getSessionFactoryOptions().getDefaultCatalog(); - } - - @Override - public String getDefaultSchema() { - return context.getSessionFactoryOptions().getDefaultCatalog(); - } - } ); + .createGenerator( context.getDialect(), null, null, context.getGeneratorSettings() ); if ( generator.generatedOnExecution() ) { throw new MappingException("must be an BeforeExecutionGenerator"); //TODO fix message } diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java index e2d878e68d..178a7aa8bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java @@ -18,7 +18,6 @@ import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.annotations.NotFoundAction; import org.hibernate.boot.spi.MetadataImplementor; -import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementHelper; import org.hibernate.bytecode.internal.BytecodeEnhancementMetadataNonPojoImpl; import org.hibernate.bytecode.internal.BytecodeEnhancementMetadataPojoImpl; @@ -35,7 +34,6 @@ import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.mapping.Component; import org.hibernate.mapping.GeneratorCreator; -import org.hibernate.mapping.GeneratorSettings; import org.hibernate.mapping.ManyToOne; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; @@ -485,19 +483,8 @@ public class EntityMetamodel implements Serializable { creationContext.getDialect(), persistentClass.getRootClass(), persistentClass.getIdentifierProperty(), - new GeneratorSettings() { - SessionFactoryOptions options() { - return creationContext.getSessionFactory().getSessionFactoryOptions(); - } - @Override - public String getDefaultCatalog() { - return options().getDefaultCatalog(); - } - @Override - public String getDefaultSchema() { - return options().getDefaultSchema(); - } - } ); + creationContext.getGeneratorSettings() + ); creationContext.getGenerators().put( rootName, idgenerator ); return idgenerator; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/LocalTemporaryTableMutationStrategyNoDropTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/LocalTemporaryTableMutationStrategyNoDropTest.java index aed5710939..a42e002b6e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/LocalTemporaryTableMutationStrategyNoDropTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/LocalTemporaryTableMutationStrategyNoDropTest.java @@ -20,6 +20,7 @@ import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.generator.Generator; +import org.hibernate.mapping.GeneratorSettings; import org.hibernate.metamodel.spi.MappingMetamodelImplementor; import org.hibernate.metamodel.spi.RuntimeModelCreationContext; import org.hibernate.query.sqm.function.SqmFunctionRegistry; @@ -101,78 +102,7 @@ public class LocalTemporaryTableMutationStrategyNoDropTest { session.getEntityPersister( null, new TestEntity() ), basename -> TemporaryTable.ID_TABLE_PREFIX + basename, dialect, - new RuntimeModelCreationContext() { - - @Override - public SessionFactoryImplementor getSessionFactory() { - return sessionFactory; - } - - @Override - public BootstrapContext getBootstrapContext() { - return null; - } - - @Override - public MetadataImplementor getBootModel() { - return scope.getMetadataImplementor(); - } - - @Override - public MappingMetamodelImplementor getDomainModel() { - return null; - } - - @Override - public SqmFunctionRegistry getFunctionRegistry() { - return null; - } - - @Override - public Map getSettings() { - return sessionFactory.getProperties(); - } - - @Override - public Dialect getDialect() { - return dialect; - } - - @Override - public CacheImplementor getCache() { - return null; - } - - @Override - public SessionFactoryOptions getSessionFactoryOptions() { - return sessionFactory.getSessionFactoryOptions(); - } - - @Override - public JdbcServices getJdbcServices() { - return jdbcServices; - } - - @Override - public SqlStringGenerationContext getSqlStringGenerationContext() { - return SqlStringGenerationContextImpl.fromExplicit( - jdbcServices.getJdbcEnvironment(), - scope.getMetadataImplementor().getDatabase(), - null, - null - ); - } - - @Override - public org.hibernate.service.ServiceRegistry getServiceRegistry() { - return sessionFactory.getServiceRegistry(); - } - - @Override - public Map getGenerators() { - return emptyMap(); - } - } + new ModelCreationContext( sessionFactory, scope, dialect, jdbcServices ) ); } @@ -211,4 +141,104 @@ public class LocalTemporaryTableMutationStrategyNoDropTest { } + + private static class ModelCreationContext implements RuntimeModelCreationContext, GeneratorSettings { + + private final SessionFactoryImplementor sessionFactory; + private final SessionFactoryScope scope; + private final Dialect dialect; + private final JdbcServices jdbcServices; + + public ModelCreationContext(SessionFactoryImplementor sessionFactory, SessionFactoryScope scope, Dialect dialect, JdbcServices jdbcServices) { + this.sessionFactory = sessionFactory; + this.scope = scope; + this.dialect = dialect; + this.jdbcServices = jdbcServices; + } + + @Override + public SessionFactoryImplementor getSessionFactory() { + return sessionFactory; + } + + @Override + public BootstrapContext getBootstrapContext() { + return null; + } + + @Override + public MetadataImplementor getBootModel() { + return scope.getMetadataImplementor(); + } + + @Override + public MappingMetamodelImplementor getDomainModel() { + return null; + } + + @Override + public SqmFunctionRegistry getFunctionRegistry() { + return null; + } + + @Override + public Map getSettings() { + return sessionFactory.getProperties(); + } + + @Override + public Dialect getDialect() { + return dialect; + } + + @Override + public CacheImplementor getCache() { + return null; + } + + @Override + public SessionFactoryOptions getSessionFactoryOptions() { + return sessionFactory.getSessionFactoryOptions(); + } + + @Override + public JdbcServices getJdbcServices() { + return jdbcServices; + } + + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + return SqlStringGenerationContextImpl.fromExplicit( + jdbcServices.getJdbcEnvironment(), + scope.getMetadataImplementor().getDatabase(), + null, + null + ); + } + + @Override + public org.hibernate.service.ServiceRegistry getServiceRegistry() { + return sessionFactory.getServiceRegistry(); + } + + @Override + public Map getGenerators() { + return emptyMap(); + } + + @Override + public String getDefaultCatalog() { + return null; + } + + @Override + public String getDefaultSchema() { + return null; + } + + @Override + public GeneratorSettings getGeneratorSettings() { + return this; + } + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/generators/UnnamedGeneratorTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/generators/UnnamedGeneratorTests.java index a1c486e5fa..40063aa30f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/generators/UnnamedGeneratorTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/generators/UnnamedGeneratorTests.java @@ -9,6 +9,8 @@ import java.util.function.Function; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; +import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; @@ -103,6 +105,11 @@ public class UnnamedGeneratorTests { public String getDefaultSchema() { return null; } + + @Override + public SqlStringGenerationContext getSqlStringGenerationContext() { + return SqlStringGenerationContextImpl.forTests( metadata.getDatabase().getJdbcEnvironment() ); + } } );