Skip to content

Commit e777604

Browse files
committed
more fallout from new Generator instantiation lifecycle
1 parent 18aa8a7 commit e777604

File tree

12 files changed

+323
-246
lines changed

12 files changed

+323
-246
lines changed

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

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.hibernate.boot.model.relational.Database;
5757
import org.hibernate.boot.model.relational.Namespace;
5858
import org.hibernate.boot.model.relational.QualifiedTableName;
59+
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
5960
import org.hibernate.boot.model.source.internal.ImplicitColumnNamingSecondPass;
6061
import org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext;
6162
import org.hibernate.boot.models.categorize.internal.ClassLoaderServiceLoading;
@@ -78,6 +79,8 @@
7879
import org.hibernate.boot.spi.SecondPass;
7980
import org.hibernate.cfg.AvailableSettings;
8081
import org.hibernate.dialect.Dialect;
82+
import org.hibernate.engine.config.spi.ConfigurationService;
83+
import org.hibernate.engine.config.spi.StandardConverters;
8184
import org.hibernate.engine.spi.FilterDefinition;
8285
import org.hibernate.engine.spi.SessionFactoryImplementor;
8386
import org.hibernate.internal.CoreLogging;
@@ -120,6 +123,9 @@
120123
import jakarta.persistence.MapsId;
121124

122125
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
126+
import static org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl.fromExplicit;
127+
import static org.hibernate.cfg.MappingSettings.DEFAULT_CATALOG;
128+
import static org.hibernate.cfg.MappingSettings.DEFAULT_SCHEMA;
123129
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
124130

125131
/**
@@ -132,7 +138,8 @@
132138
*
133139
* @author Steve Ebersole
134140
*/
135-
public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector, ConverterRegistry {
141+
public class InFlightMetadataCollectorImpl
142+
implements InFlightMetadataCollector, ConverterRegistry, GeneratorSettings {
136143
private static final CoreMessageLogger log = CoreLogging.messageLogger( InFlightMetadataCollectorImpl.class );
137144

138145
private final BootstrapContext bootstrapContext;
@@ -170,6 +177,8 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
170177

171178
private Map<String, SqmFunctionDescriptor> sqlFunctionMap;
172179

180+
final ConfigurationService configurationService;
181+
173182
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174183
// All the annotation-processing-specific state :(
175184
private final Set<String> defaultIdentifierGeneratorNames = new HashSet<>();
@@ -210,16 +219,12 @@ public InFlightMetadataCollectorImpl(
210219
}
211220

212221
bootstrapContext.getAuxiliaryDatabaseObjectList().forEach( getDatabase()::addAuxiliaryDatabaseObject );
222+
223+
configurationService = bootstrapContext.getServiceRegistry().requireService(ConfigurationService.class);
213224
}
214225

215-
public InFlightMetadataCollectorImpl(
216-
BootstrapContext bootstrapContext,
217-
MetadataBuildingOptions options) {
218-
this(
219-
bootstrapContext,
220-
createModelBuildingContext( bootstrapContext ),
221-
options
222-
);
226+
public InFlightMetadataCollectorImpl(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
227+
this( bootstrapContext, createModelBuildingContext( bootstrapContext ), options );
223228
}
224229

225230
private static SourceModelBuildingContext createModelBuildingContext(BootstrapContext bootstrapContext) {
@@ -2204,20 +2209,7 @@ private void processExportableProducers() {
22042209
private void handleIdentifierValueBinding(
22052210
KeyValue identifierValueBinding, Dialect dialect, RootClass entityBinding, Property identifierProperty) {
22062211
try {
2207-
identifierValueBinding.createGenerator( dialect, entityBinding, identifierProperty,
2208-
new GeneratorSettings() {
2209-
@Override
2210-
public String getDefaultCatalog() {
2211-
//TODO: does not have access to property-configured default
2212-
return persistenceUnitMetadata.getDefaultCatalog();
2213-
}
2214-
2215-
@Override
2216-
public String getDefaultSchema() {
2217-
//TODO: does not have access to property-configured default
2218-
return persistenceUnitMetadata.getDefaultSchema();
2219-
}
2220-
} );
2212+
identifierValueBinding.createGenerator( dialect, entityBinding, identifierProperty, this );
22212213
}
22222214
catch (MappingException e) {
22232215
// ignore this for now. The reasoning being "non-reflective" binding as needed
@@ -2227,4 +2219,21 @@ public String getDefaultSchema() {
22272219
log.debugf( "Ignoring exception thrown when trying to build IdentifierGenerator as part of Metadata building", e );
22282220
}
22292221
}
2222+
2223+
@Override
2224+
public String getDefaultCatalog() {
2225+
final String defaultCatalog = configurationService.getSetting( DEFAULT_CATALOG, StandardConverters.STRING );
2226+
return defaultCatalog == null ? persistenceUnitMetadata.getDefaultCatalog() : defaultCatalog;
2227+
}
2228+
2229+
@Override
2230+
public String getDefaultSchema() {
2231+
final String defaultSchema = configurationService.getSetting( DEFAULT_SCHEMA, StandardConverters.STRING );
2232+
return defaultSchema == null ? persistenceUnitMetadata.getDefaultSchema() : defaultSchema;
2233+
}
2234+
2235+
@Override
2236+
public SqlStringGenerationContext getSqlStringGenerationContext() {
2237+
return fromExplicit( database.getJdbcEnvironment(), database, getDefaultCatalog(), getDefaultSchema() );
2238+
}
22302239
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ default boolean getNativeJdbcParametersIgnored() {
244244
* neither explicitly nor implicitly (see the concept of implicit catalog in XML mapping).
245245
*
246246
* @return The default catalog to use.
247+
*
248+
* @see org.hibernate.cfg.MappingSettings#DEFAULT_CATALOG
247249
*/
248250
default String getDefaultCatalog() {
249251
return null;
@@ -254,6 +256,8 @@ default String getDefaultCatalog() {
254256
* neither explicitly nor implicitly (see the concept of implicit schema in XML mapping).
255257
*
256258
* @return The default schema to use.
259+
*
260+
* @see org.hibernate.cfg.MappingSettings#DEFAULT_SCHEMA
257261
*/
258262
default String getDefaultSchema() {
259263
return null;

hibernate-core/src/main/java/org/hibernate/generator/GeneratorCreationContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
@Incubating
2929
public interface GeneratorCreationContext {
3030
/**
31-
* View of the relational database objects (tables, sequences, ...) and namespaces (catalogs and schemas).
31+
* View of the relational database objects (tables, sequences, etc.)
32+
* and namespaces (catalogs and schemas). Generators may add new
33+
* tables or sequences to the returned {@link Database}.
3234
*/
3335
Database getDatabase();
3436

@@ -69,6 +71,9 @@ default Type getType() {
6971
return getProperty().getType();
7072
}
7173

74+
/**
75+
* The {@link SqlStringGenerationContext} to use when generating SQL.
76+
*/
7277
default SqlStringGenerationContext getSqlStringGenerationContext() {
7378
final Database database = getDatabase();
7479
return fromExplicit( database.getJdbcEnvironment(), database, getDefaultCatalog(), getDefaultSchema() );

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

Lines changed: 111 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import org.hibernate.jpa.internal.ExceptionMapperLegacyJpaImpl;
7373
import org.hibernate.jpa.internal.PersistenceUnitUtilImpl;
7474
import org.hibernate.mapping.Collection;
75+
import org.hibernate.mapping.GeneratorSettings;
7576
import org.hibernate.mapping.PersistentClass;
7677
import org.hibernate.mapping.RootClass;
7778
import org.hibernate.metamodel.internal.RuntimeMetamodelsImpl;
@@ -277,16 +278,16 @@ public SessionFactoryImpl(
277278
// create runtime metamodels (mapping and JPA)
278279
final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl();
279280
runtimeMetamodels = runtimeMetamodelsImpl;
280-
final MappingMetamodelImpl mappingMetamodelImpl =
281-
new MappingMetamodelImpl( typeConfiguration, serviceRegistry );
281+
final MappingMetamodelImpl mappingMetamodelImpl = new MappingMetamodelImpl( typeConfiguration, serviceRegistry );
282282
runtimeMetamodelsImpl.setMappingMetamodel( mappingMetamodelImpl );
283283
fastSessionServices = new FastSessionServices( this );
284-
initializeMappingModel( mappingMetamodelImpl, bootstrapContext, bootMetamodel, options );
284+
mappingMetamodelImpl.finishInitialization(
285+
new ModelCreationContext( bootstrapContext, bootMetamodel, mappingMetamodelImpl, typeConfiguration ) );
285286
runtimeMetamodelsImpl.setJpaMetamodel( mappingMetamodelImpl.getJpaMetamodel() );
286287

287288
// this needs to happen after the mapping metamodel is
288289
// completely built, since we need to use the persisters
289-
fetchProfiles = getFetchProfiles( bootMetamodel, runtimeMetamodels );
290+
fetchProfiles = getFetchProfiles( bootMetamodel, runtimeMetamodelsImpl);
290291

291292
defaultSessionOpenOptions = createDefaultSessionOpenOptionsIfPossible();
292293
temporarySessionOpenOptions = defaultSessionOpenOptions == null ? null : buildTemporarySessionOpenOptions();
@@ -320,102 +321,6 @@ public SessionFactoryImpl(
320321
LOG.debug( "Instantiated SessionFactory" );
321322
}
322323

323-
private void initializeMappingModel(
324-
MappingMetamodelImpl mappingMetamodelImpl,
325-
BootstrapContext bootstrapContext,
326-
MetadataImplementor bootMetamodel,
327-
SessionFactoryOptions options) {
328-
mappingMetamodelImpl.finishInitialization( runtimeModelCreationContext(
329-
bootstrapContext,
330-
bootMetamodel,
331-
mappingMetamodelImpl,
332-
mappingMetamodelImpl.getTypeConfiguration(),
333-
options
334-
) );
335-
}
336-
337-
private RuntimeModelCreationContext runtimeModelCreationContext(
338-
BootstrapContext bootstrapContext,
339-
MetadataImplementor bootMetamodel,
340-
MappingMetamodelImplementor mappingMetamodel,
341-
TypeConfiguration typeConfiguration,
342-
SessionFactoryOptions options) {
343-
return new RuntimeModelCreationContext() {
344-
final Map<String,Generator> generators = new HashMap<>();
345-
346-
@Override
347-
public BootstrapContext getBootstrapContext() {
348-
return bootstrapContext;
349-
}
350-
351-
@Override
352-
public SessionFactoryImplementor getSessionFactory() {
353-
// this is bad, we're not yet fully-initialized
354-
return SessionFactoryImpl.this;
355-
}
356-
357-
@Override
358-
public MetadataImplementor getBootModel() {
359-
return bootMetamodel;
360-
}
361-
362-
@Override
363-
public MappingMetamodelImplementor getDomainModel() {
364-
return mappingMetamodel;
365-
}
366-
367-
@Override
368-
public CacheImplementor getCache() {
369-
return cacheAccess;
370-
}
371-
372-
@Override
373-
public Map<String, Object> getSettings() {
374-
return settings;
375-
}
376-
377-
@Override
378-
public Dialect getDialect() {
379-
return jdbcServices.getDialect();
380-
}
381-
382-
@Override
383-
public SqmFunctionRegistry getFunctionRegistry() {
384-
return queryEngine.getSqmFunctionRegistry();
385-
}
386-
387-
@Override
388-
public TypeConfiguration getTypeConfiguration() {
389-
return typeConfiguration;
390-
}
391-
392-
@Override
393-
public SessionFactoryOptions getSessionFactoryOptions() {
394-
return options;
395-
}
396-
397-
@Override
398-
public JdbcServices getJdbcServices() {
399-
return jdbcServices;
400-
}
401-
402-
@Override
403-
public SqlStringGenerationContext getSqlStringGenerationContext() {
404-
return sqlStringGenerationContext;
405-
}
406-
407-
@Override
408-
public ServiceRegistry getServiceRegistry() {
409-
return serviceRegistry;
410-
}
411-
412-
@Override
413-
public Map<String, Generator> getGenerators() {
414-
return generators;
415-
}
416-
};
417-
}
418-
419324
private static SqlStringGenerationContext createSqlStringGenerationContext(
420325
MetadataImplementor bootMetamodel,
421326
SessionFactoryOptions options,
@@ -1687,4 +1592,110 @@ private enum Status {
16871592
CLOSING,
16881593
CLOSED
16891594
}
1595+
1596+
private class ModelCreationContext implements RuntimeModelCreationContext, GeneratorSettings {
1597+
final Map<String, Generator> generators;
1598+
private final BootstrapContext bootstrapContext;
1599+
private final MetadataImplementor bootMetamodel;
1600+
private final MappingMetamodelImplementor mappingMetamodel;
1601+
private final TypeConfiguration typeConfiguration;
1602+
1603+
private ModelCreationContext(
1604+
BootstrapContext bootstrapContext,
1605+
MetadataImplementor bootMetamodel,
1606+
MappingMetamodelImplementor mappingMetamodel,
1607+
TypeConfiguration typeConfiguration) {
1608+
this.bootstrapContext = bootstrapContext;
1609+
this.bootMetamodel = bootMetamodel;
1610+
this.mappingMetamodel = mappingMetamodel;
1611+
this.typeConfiguration = typeConfiguration;
1612+
generators = new HashMap<>();
1613+
}
1614+
1615+
@Override
1616+
public BootstrapContext getBootstrapContext() {
1617+
return bootstrapContext;
1618+
}
1619+
1620+
@Override
1621+
public SessionFactoryImplementor getSessionFactory() {
1622+
// this is bad, we're not yet fully-initialized
1623+
return SessionFactoryImpl.this;
1624+
}
1625+
1626+
@Override
1627+
public MetadataImplementor getBootModel() {
1628+
return bootMetamodel;
1629+
}
1630+
1631+
@Override
1632+
public MappingMetamodelImplementor getDomainModel() {
1633+
return mappingMetamodel;
1634+
}
1635+
1636+
@Override
1637+
public CacheImplementor getCache() {
1638+
return cacheAccess;
1639+
}
1640+
1641+
@Override
1642+
public Map<String, Object> getSettings() {
1643+
return settings;
1644+
}
1645+
1646+
@Override
1647+
public Dialect getDialect() {
1648+
return jdbcServices.getDialect();
1649+
}
1650+
1651+
@Override
1652+
public SqmFunctionRegistry getFunctionRegistry() {
1653+
return queryEngine.getSqmFunctionRegistry();
1654+
}
1655+
1656+
@Override
1657+
public TypeConfiguration getTypeConfiguration() {
1658+
return typeConfiguration;
1659+
}
1660+
1661+
@Override
1662+
public SessionFactoryOptions getSessionFactoryOptions() {
1663+
return sessionFactoryOptions;
1664+
}
1665+
1666+
@Override
1667+
public JdbcServices getJdbcServices() {
1668+
return jdbcServices;
1669+
}
1670+
1671+
@Override
1672+
public ServiceRegistry getServiceRegistry() {
1673+
return serviceRegistry;
1674+
}
1675+
1676+
@Override
1677+
public Map<String, Generator> getGenerators() {
1678+
return generators;
1679+
}
1680+
1681+
@Override
1682+
public GeneratorSettings getGeneratorSettings() {
1683+
return this;
1684+
}
1685+
1686+
@Override
1687+
public String getDefaultCatalog() {
1688+
return sessionFactoryOptions.getDefaultCatalog();
1689+
}
1690+
1691+
@Override
1692+
public String getDefaultSchema() {
1693+
return sessionFactoryOptions.getDefaultSchema();
1694+
}
1695+
1696+
@Override
1697+
public SqlStringGenerationContext getSqlStringGenerationContext() {
1698+
return sqlStringGenerationContext;
1699+
}
1700+
}
16901701
}

0 commit comments

Comments
 (0)