Skip to content

Commit 18aa8a7

Browse files
committed
don't cache the Generator in the SimpleValue
that way we don't need to re-call initialize() on the cached instance also handle canonicalization of generators via RuntimeModelCreationContext instead of via deprecated method or SessionFactoryImplementor
1 parent 8ee0948 commit 18aa8a7

File tree

12 files changed

+114
-93
lines changed

12 files changed

+114
-93
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,6 @@ private void processExportableProducers() {
22042204
private void handleIdentifierValueBinding(
22052205
KeyValue identifierValueBinding, Dialect dialect, RootClass entityBinding, Property identifierProperty) {
22062206
try {
2207-
// this call typically caches the new Generator in the instance of KeyValue
22082207
identifierValueBinding.createGenerator( dialect, entityBinding, identifierProperty,
22092208
new GeneratorSettings() {
22102209
@Override

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

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@
6767
import org.hibernate.event.spi.EventEngine;
6868
import org.hibernate.generator.Generator;
6969
import org.hibernate.graph.spi.RootGraphImplementor;
70-
import org.hibernate.id.Configurable;
7170
import org.hibernate.integrator.spi.Integrator;
7271
import org.hibernate.integrator.spi.IntegratorService;
7372
import org.hibernate.jpa.internal.ExceptionMapperLegacyJpaImpl;
7473
import org.hibernate.jpa.internal.PersistenceUnitUtilImpl;
7574
import org.hibernate.mapping.Collection;
76-
import org.hibernate.mapping.GeneratorSettings;
7775
import org.hibernate.mapping.PersistentClass;
7876
import org.hibernate.mapping.RootClass;
7977
import org.hibernate.metamodel.internal.RuntimeMetamodelsImpl;
@@ -185,8 +183,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
185183

186184
private final transient CurrentSessionContext currentSessionContext;
187185

188-
// todo : move to MetamodelImpl
189-
private final transient Map<String, Generator> identifierGenerators;
190186
private final transient Map<String, FilterDefinition> filters;
191187
private final transient java.util.Collection<FilterDefinition> autoEnabledFilters = new HashSet<>();
192188
private final transient Map<String, FetchProfile> fetchProfiles;
@@ -263,7 +259,6 @@ public SessionFactoryImpl(
263259
try {
264260
integrate( bootMetamodel, bootstrapContext, integratorObserver );
265261

266-
identifierGenerators = createGenerators( jdbcServices, bootMetamodel, options );
267262
bootMetamodel.orderColumns( false );
268263
bootMetamodel.validate();
269264

@@ -346,6 +341,8 @@ private RuntimeModelCreationContext runtimeModelCreationContext(
346341
TypeConfiguration typeConfiguration,
347342
SessionFactoryOptions options) {
348343
return new RuntimeModelCreationContext() {
344+
final Map<String,Generator> generators = new HashMap<>();
345+
349346
@Override
350347
public BootstrapContext getBootstrapContext() {
351348
return bootstrapContext;
@@ -411,41 +408,12 @@ public SqlStringGenerationContext getSqlStringGenerationContext() {
411408
public ServiceRegistry getServiceRegistry() {
412409
return serviceRegistry;
413410
}
414-
};
415-
}
416411

417-
private Map<String, Generator> createGenerators(
418-
JdbcServices jdbcServices, MetadataImplementor metamodel, SessionFactoryOptions options) {
419-
final Dialect dialect = jdbcServices.getJdbcEnvironment().getDialect();
420-
final Map<String, Generator> generators = new HashMap<>();
421-
for ( PersistentClass model : metamodel.getEntityBindings() ) {
422-
if ( model instanceof RootClass rootClass ) {
423-
final Generator generator =
424-
model.getIdentifier()
425-
// returns the cached Generator if it was already created
426-
.createGenerator( dialect, rootClass, model.getIdentifierProperty(),
427-
new GeneratorSettings() {
428-
@Override
429-
public String getDefaultCatalog() {
430-
return options.getDefaultCatalog();
431-
}
432-
433-
@Override
434-
public String getDefaultSchema() {
435-
return options.getDefaultSchema();
436-
}
437-
} );
438-
// the cached generator might have been created from
439-
// InFlightMetadataCollectorImpl.handleIdentifierValueBinding,
440-
// in which case it did not have access to the property-configured
441-
// default catalog and schema, so re-render SQL here
442-
if ( generator instanceof Configurable configurable ) {
443-
configurable.initialize( sqlStringGenerationContext );
444-
}
445-
generators.put( model.getEntityName(), generator );
412+
@Override
413+
public Map<String, Generator> getGenerators() {
414+
return generators;
446415
}
447-
}
448-
return generators;
416+
};
449417
}
450418

451419
private static SqlStringGenerationContext createSqlStringGenerationContext(
@@ -1086,7 +1054,7 @@ public Set<String> getDefinedFetchProfileNames() {
10861054

10871055
@Override @Deprecated
10881056
public Generator getGenerator(String rootEntityName) {
1089-
return identifierGenerators.get( rootEntityName );
1057+
return null;
10901058
}
10911059

10921060
private boolean canAccessTransactionManager() {

hibernate-core/src/main/java/org/hibernate/mapping/Component.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ public class Component extends SimpleValue implements MetaAttributable, Sortable
106106
private transient Class<?> componentClass;
107107
private transient Boolean simpleRecord;
108108

109-
private transient Generator builtIdentifierGenerator;
110-
111109
public Component(MetadataBuildingContext metadata, PersistentClass owner) throws MappingException {
112110
this( metadata, owner.getTable(), owner );
113111
}
@@ -652,13 +650,9 @@ public String toString() {
652650

653651
@Override
654652
public Generator createGenerator(Dialect dialect, RootClass rootClass, Property property, GeneratorSettings defaults) {
655-
if ( builtIdentifierGenerator == null ) {
656-
builtIdentifierGenerator =
657-
getCustomIdGeneratorCreator().isAssigned()
658-
? buildIdentifierGenerator( dialect, rootClass, defaults )
659-
: super.createGenerator( dialect, rootClass, property, defaults );
660-
}
661-
return builtIdentifierGenerator;
653+
return getCustomIdGeneratorCreator().isAssigned()
654+
? buildIdentifierGenerator( dialect, rootClass, defaults )
655+
: super.createGenerator( dialect, rootClass, property, defaults );
662656
}
663657

664658
private Generator buildIdentifierGenerator(Dialect dialect, RootClass rootClass, GeneratorSettings defaults) {

hibernate-core/src/main/java/org/hibernate/mapping/KeyValue.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ public interface KeyValue extends Value {
2727

2828
@Deprecated(since = "7.0")
2929
default Generator createGenerator(Dialect dialect, RootClass rootClass) {
30-
return createGenerator( dialect, rootClass, null, null );
30+
return createGenerator( dialect, rootClass, null, new GeneratorSettings() {
31+
@Override
32+
public String getDefaultCatalog() {
33+
return null;
34+
}
35+
36+
@Override
37+
public String getDefaultSchema() {
38+
return null;
39+
}
40+
} );
3141
}
3242

3343
Generator createGenerator(

hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public abstract class SimpleValue implements KeyValue {
103103
private Type type;
104104

105105
private GeneratorCreator customIdGeneratorCreator = ASSIGNED_IDENTIFIER_GENERATOR_CREATOR;
106-
private Generator generator;
107106

108107
public SimpleValue(MetadataBuildingContext buildingContext) {
109108
this.buildingContext = buildingContext;
@@ -137,7 +136,6 @@ protected SimpleValue(SimpleValue original) {
137136
this.attributeConverterDescriptor = original.attributeConverterDescriptor;
138137
this.type = original.type;
139138
this.customIdGeneratorCreator = original.customIdGeneratorCreator;
140-
this.generator = original.generator;
141139
}
142140

143141
@Override
@@ -381,17 +379,18 @@ public Generator createGenerator(
381379
RootClass rootClass,
382380
Property property,
383381
GeneratorSettings defaults) {
384-
if ( generator == null ) {
385-
if ( customIdGeneratorCreator != null ) {
386-
final IdGeneratorCreationContext context =
387-
new IdGeneratorCreationContext( rootClass, property, defaults );
388-
generator = customIdGeneratorCreator.createGenerator( context );
389-
if ( generator.allowAssignedIdentifiers() && getNullValue() == null ) {
390-
setNullValue( "undefined" );
391-
}
382+
if ( customIdGeneratorCreator != null ) {
383+
final IdGeneratorCreationContext context =
384+
new IdGeneratorCreationContext( rootClass, property, defaults );
385+
final Generator generator = customIdGeneratorCreator.createGenerator( context );
386+
if ( generator.allowAssignedIdentifiers() && getNullValue() == null ) {
387+
setNullValue( "undefined" );
392388
}
389+
return generator;
390+
}
391+
else {
392+
return null;
393393
}
394-
return generator;
395394
}
396395

397396
@Internal

hibernate-core/src/main/java/org/hibernate/metamodel/spi/RuntimeModelCreationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.hibernate.dialect.Dialect;
1313
import org.hibernate.engine.jdbc.spi.JdbcServices;
1414
import org.hibernate.engine.spi.SessionFactoryImplementor;
15+
import org.hibernate.generator.Generator;
1516
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
1617
import org.hibernate.service.ServiceRegistry;
1718
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
@@ -58,4 +59,6 @@ default MetadataImplementor getMetadata() {
5859
SqlStringGenerationContext getSqlStringGenerationContext();
5960

6061
ServiceRegistry getServiceRegistry();
62+
63+
Map<String, Generator> getGenerators();
6164
}

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -909,15 +909,11 @@ private static CacheLayout queryCacheLayout(CacheLayout entityQueryCacheLayout,
909909
}
910910

911911
private boolean shouldUseShallowCacheLayout(CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) {
912-
switch ( queryCacheLayout( entityQueryCacheLayout, options ) ) {
913-
case FULL:
914-
return false;
915-
case AUTO:
916-
return canUseReferenceCacheEntries()
917-
|| canReadFromCache();
918-
default:
919-
return true;
920-
}
912+
return switch ( queryCacheLayout( entityQueryCacheLayout, options ) ) {
913+
case FULL -> false;
914+
case AUTO -> canUseReferenceCacheEntries() || canReadFromCache();
915+
default -> true;
916+
};
921917
}
922918

923919
private static boolean shouldStoreDiscriminatorInShallowQueryCacheLayout(

hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.MappingException;
1919
import org.hibernate.annotations.NotFoundAction;
2020
import org.hibernate.boot.spi.MetadataImplementor;
21+
import org.hibernate.boot.spi.SessionFactoryOptions;
2122
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementHelper;
2223
import org.hibernate.bytecode.internal.BytecodeEnhancementMetadataNonPojoImpl;
2324
import org.hibernate.bytecode.internal.BytecodeEnhancementMetadataPojoImpl;
@@ -34,6 +35,7 @@
3435
import org.hibernate.internal.util.collections.ArrayHelper;
3536
import org.hibernate.mapping.Component;
3637
import org.hibernate.mapping.GeneratorCreator;
38+
import org.hibernate.mapping.GeneratorSettings;
3739
import org.hibernate.mapping.ManyToOne;
3840
import org.hibernate.mapping.PersistentClass;
3941
import org.hibernate.mapping.Property;
@@ -160,10 +162,8 @@ public EntityMetamodel(
160162

161163
subclassId = persistentClass.getSubclassId();
162164

163-
identifierAttribute = PropertyFactory.buildIdentifierAttribute(
164-
persistentClass,
165-
sessionFactory.getGenerator( rootName )
166-
);
165+
final Generator idgenerator = buildIdGenerator( persistentClass, creationContext );
166+
identifierAttribute = PropertyFactory.buildIdentifierAttribute( persistentClass, idgenerator );
167167

168168
versioned = persistentClass.isVersioned();
169169

@@ -472,6 +472,37 @@ && isAbstractClass( persistentClass.getMappedClass() ) ) {
472472
// entityNameByInheritanceClassMap = toSmallMap( entityNameByInheritanceClassMapLocal );
473473
}
474474

475+
private Generator buildIdGenerator(PersistentClass persistentClass, RuntimeModelCreationContext creationContext) {
476+
final Generator existing = creationContext.getGenerators().get( rootName );
477+
if ( existing != null ) {
478+
return existing;
479+
}
480+
else {
481+
final Generator idgenerator =
482+
persistentClass.getIdentifier()
483+
// returns the cached Generator if it was already created
484+
.createGenerator(
485+
creationContext.getDialect(),
486+
persistentClass.getRootClass(),
487+
persistentClass.getIdentifierProperty(),
488+
new GeneratorSettings() {
489+
SessionFactoryOptions options() {
490+
return creationContext.getSessionFactory().getSessionFactoryOptions();
491+
}
492+
@Override
493+
public String getDefaultCatalog() {
494+
return options().getDefaultCatalog();
495+
}
496+
@Override
497+
public String getDefaultSchema() {
498+
return options().getDefaultSchema();
499+
}
500+
} );
501+
creationContext.getGenerators().put( rootName, idgenerator );
502+
return idgenerator;
503+
}
504+
}
505+
475506
private void verifyNaturalIdProperty(Property property) {
476507
final Value value = property.getValue();
477508
if ( value instanceof ManyToOne toOne ) {

hibernate-core/src/test/java/org/hibernate/orm/test/LocalTemporaryTableMutationStrategyNoDropTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hibernate.engine.jdbc.spi.JdbcServices;
2020
import org.hibernate.engine.spi.SessionFactoryImplementor;
2121
import org.hibernate.engine.spi.SessionImplementor;
22+
import org.hibernate.generator.Generator;
2223
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
2324
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
2425
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
@@ -40,6 +41,7 @@
4041
import jakarta.persistence.Inheritance;
4142
import jakarta.persistence.InheritanceType;
4243

44+
import static java.util.Collections.emptyMap;
4345
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4446

4547
@DomainModel(
@@ -165,6 +167,11 @@ public SqlStringGenerationContext getSqlStringGenerationContext() {
165167
public org.hibernate.service.ServiceRegistry getServiceRegistry() {
166168
return sessionFactory.getServiceRegistry();
167169
}
170+
171+
@Override
172+
public Map<String, Generator> getGenerators() {
173+
return emptyMap();
174+
}
168175
}
169176
);
170177
}

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/generators/UnnamedGeneratorTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1414
import org.hibernate.cfg.AvailableSettings;
1515
import org.hibernate.generator.Generator;
16+
import org.hibernate.mapping.GeneratorSettings;
1617
import org.hibernate.mapping.RootClass;
1718

1819
import org.junit.jupiter.api.Test;
@@ -92,7 +93,17 @@ private void withGenerator(Class<?> entityClass, boolean strictlyGlobal, Consume
9293
metadata.getDatabase().getDialect(),
9394
entityBinding,
9495
entityBinding.getIdentifierProperty(),
95-
null
96+
new GeneratorSettings() {
97+
@Override
98+
public String getDefaultCatalog() {
99+
return null;
100+
}
101+
102+
@Override
103+
public String getDefaultSchema() {
104+
return null;
105+
}
106+
}
96107
);
97108

98109
checks.accept( generator );

0 commit comments

Comments
 (0)