Skip to content

Commit 562d2c9

Browse files
gavinkingsebersole
authored andcommitted
HHH-18139 remove identifierGeneratorStrategy/identifierGeneratorParameters from SimpleValue
Signed-off-by: Gavin King <gavin@hibernate.org>
1 parent b750ee9 commit 562d2c9

File tree

19 files changed

+190
-268
lines changed

19 files changed

+190
-268
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/IdentifierGeneratorDefinition.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import static java.util.Collections.emptyMap;
2828
import static java.util.Collections.unmodifiableMap;
29+
import static org.hibernate.boot.internal.GenerationStrategyInterpreter.STRATEGY_INTERPRETER;
30+
import static org.hibernate.boot.models.JpaAnnotations.SEQUENCE_GENERATOR;
2931
import static org.hibernate.boot.models.JpaAnnotations.TABLE_GENERATOR;
3032
import static org.hibernate.boot.models.internal.AnnotationUsageHelper.applyStringAttributeIfSpecified;
3133
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
@@ -123,7 +125,7 @@ public static IdentifierGeneratorDefinition createImplicit(
123125
);
124126
}
125127
case AUTO: {
126-
final String strategyName = GenerationStrategyInterpreter.STRATEGY_INTERPRETER.determineGeneratorName(
128+
final String strategyName = STRATEGY_INTERPRETER.determineGeneratorName(
127129
generationType,
128130
new GenerationStrategyInterpreter.GeneratorNameDeterminationContext() {
129131
@Override
@@ -153,15 +155,15 @@ private static IdentifierGeneratorDefinition buildTableGeneratorDefinition(Strin
153155
final Builder builder = new Builder();
154156
final MutableAnnotationUsage<TableGenerator> tableGeneratorUsage = TABLE_GENERATOR.createUsage( null );
155157
tableGeneratorUsage.setAttributeValue( "name", name );
156-
GenerationStrategyInterpreter.STRATEGY_INTERPRETER.interpretTableGenerator( tableGeneratorUsage, builder );
158+
STRATEGY_INTERPRETER.interpretTableGenerator( tableGeneratorUsage, builder );
157159
return builder.build();
158160
}
159161

160162
private static IdentifierGeneratorDefinition buildSequenceGeneratorDefinition(String name) {
161163
final Builder builder = new Builder();
162-
final MutableAnnotationUsage<SequenceGenerator> sequenceGeneratorUsage = JpaAnnotations.SEQUENCE_GENERATOR.createUsage( null );
164+
final MutableAnnotationUsage<SequenceGenerator> sequenceGeneratorUsage = SEQUENCE_GENERATOR.createUsage( null );
163165
applyStringAttributeIfSpecified( "name", name, sequenceGeneratorUsage );
164-
GenerationStrategyInterpreter.STRATEGY_INTERPRETER.interpretSequenceGenerator( sequenceGeneratorUsage, builder );
166+
STRATEGY_INTERPRETER.interpretSequenceGenerator( sequenceGeneratorUsage, builder );
165167
return builder.build();
166168
}
167169

hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
2828
import org.hibernate.boot.spi.InFlightMetadataCollector;
2929
import org.hibernate.boot.spi.MetadataBuildingContext;
30-
import org.hibernate.boot.spi.PropertyData;
3130
import org.hibernate.cfg.AvailableSettings;
3231
import org.hibernate.dialect.Dialect;
3332
import org.hibernate.engine.config.spi.ConfigurationService;
@@ -77,24 +76,24 @@
7776
import jakarta.persistence.TableGenerator;
7877
import jakarta.persistence.Version;
7978

79+
import static java.util.Collections.emptyMap;
8080
import static org.hibernate.boot.model.internal.AnnotationHelper.extractParameterMap;
8181
import static org.hibernate.boot.model.internal.BinderHelper.isCompositeId;
8282
import static org.hibernate.boot.model.internal.BinderHelper.isGlobalGeneratorNameGlobal;
8383
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
84-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
8584
import static org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY;
8685

8786
public class GeneratorBinder {
8887

8988
private static final Logger LOG = CoreLogging.logger( BinderHelper.class );
9089

9190
public static Generator createLegacyIdentifierGenerator(
91+
String strategy,
9292
SimpleValue simpleValue,
9393
Dialect dialect,
94-
String defaultCatalog,
95-
String defaultSchema,
96-
RootClass rootClass) {
97-
final Class<? extends Generator> generatorClass = generatorClass( simpleValue );
94+
RootClass rootClass,
95+
Map<String, Object> configuration) {
96+
final Class<? extends Generator> generatorClass = generatorClass( strategy, simpleValue );
9897
final Constructor<? extends Generator> defaultConstructor = getDefaultConstructor( generatorClass );
9998
if ( defaultConstructor == null ) {
10099
throw new org.hibernate.InstantiationException( "No default constructor for id generator class", generatorClass );
@@ -107,15 +106,14 @@ public static Generator createLegacyIdentifierGenerator(
107106
throw new org.hibernate.InstantiationException( "Could not instantiate id generator", generatorClass, e );
108107
}
109108
if ( identifierGenerator instanceof Configurable ) {
110-
final Properties parameters = collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass );
109+
final Properties parameters = collectParameters( simpleValue, dialect, rootClass, configuration );
111110
final Configurable configurable = (Configurable) identifierGenerator;
112111
configurable.configure( simpleValue.getType(), parameters, simpleValue.getServiceRegistry() );
113112
}
114113
return identifierGenerator;
115114
}
116115

117-
private static Class<? extends Generator> generatorClass(SimpleValue simpleValue) {
118-
String strategy = simpleValue.getIdentifierGeneratorStrategy();
116+
private static Class<? extends Generator> generatorClass(String strategy, SimpleValue simpleValue) {
119117
if ( "native".equals(strategy) ) {
120118
strategy =
121119
simpleValue.getMetadata().getDatabase().getDialect()
@@ -160,26 +158,14 @@ private static Class<? extends Generator> generatorClass(SimpleValue simpleValue
160158
public static Properties collectParameters(
161159
SimpleValue simpleValue,
162160
Dialect dialect,
163-
String defaultCatalog,
164-
String defaultSchema,
165-
RootClass rootClass) {
161+
RootClass rootClass,
162+
Map<String, Object> configuration) {
166163
final ConfigurationService configService =
167164
simpleValue.getMetadata().getMetadataBuildingOptions().getServiceRegistry()
168165
.requireService( ConfigurationService.class );
169166

170167
final Properties params = new Properties();
171168

172-
// This is for backwards compatibility only;
173-
// when this method is called by Hibernate ORM, defaultSchema and defaultCatalog are always
174-
// null, and defaults are handled later.
175-
if ( defaultSchema != null ) {
176-
params.setProperty( PersistentIdentifierGenerator.SCHEMA, defaultSchema);
177-
}
178-
179-
if ( defaultCatalog != null ) {
180-
params.setProperty( PersistentIdentifierGenerator.CATALOG, defaultCatalog);
181-
}
182-
183169
// default initial value and allocation size per-JPA defaults
184170
params.setProperty( OptimizableGenerator.INITIAL_PARAM,
185171
String.valueOf( OptimizableGenerator.DEFAULT_INITIAL_VALUE ) );
@@ -216,12 +202,6 @@ public static Properties collectParameters(
216202
params.setProperty( OptimizableGenerator.IMPLICIT_NAME_BASE, tableName );
217203
}
218204

219-
if ( simpleValue.getIdentifierGeneratorParameters() != null ) {
220-
params.putAll( simpleValue.getIdentifierGeneratorParameters() );
221-
}
222-
223-
// TODO : we should pass along all settings once "config lifecycle" is hashed out...
224-
225205
params.put( IdentifierGenerator.CONTRIBUTOR_NAME,
226206
simpleValue.getBuildingContext().getCurrentContributorName() );
227207

@@ -231,14 +211,16 @@ public static Properties collectParameters(
231211
settings.get( AvailableSettings.PREFERRED_POOLED_OPTIMIZER ) );
232212
}
233213

214+
params.putAll( configuration );
215+
234216
return params;
235217
}
236218

237219
private static String identityTablesString(Dialect dialect, RootClass rootClass) {
238220
final StringBuilder tables = new StringBuilder();
239221
for ( Table table : rootClass.getIdentityTables() ) {
240222
tables.append( table.getQuotedName( dialect ) );
241-
if ( tables.length()>0 ) {
223+
if ( !tables.isEmpty() ) {
242224
tables.append( ", " );
243225
}
244226
}
@@ -286,6 +268,7 @@ public static void makeIdGenerator(
286268
parameters.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, buildingContext.getObjectNameNormalizer() );
287269
parameters.put( IdentifierGenerator.GENERATOR_NAME, generatorName );
288270

271+
final String generatorStrategy;
289272
if ( !generatorName.isEmpty() ) {
290273
//we have a named generator
291274
final IdentifierGeneratorDefinition definition =
@@ -296,22 +279,24 @@ public static void makeIdGenerator(
296279
+ " (define a named generator using '@SequenceGenerator', '@TableGenerator', or '@GenericGenerator')" );
297280
}
298281
//This is quite vague in the spec but a generator could override the generator choice
299-
final String identifierGeneratorStrategy = definition.getStrategy();
300282
//yuk! this is a hack not to override 'AUTO' even if generator is set
301283
final boolean avoidOverriding =
302-
identifierGeneratorStrategy.equals( "identity" )
303-
|| identifierGeneratorStrategy.equals( "seqhilo" );
284+
definition.getStrategy().equals( "identity" )
285+
|| definition.getStrategy().equals( "seqhilo" );
304286
if ( generatorType == null || !avoidOverriding ) {
305-
generatorType = identifierGeneratorStrategy;
287+
generatorStrategy = definition.getStrategy();
288+
}
289+
else {
290+
generatorStrategy = generatorType;
306291
}
307292
//checkIfMatchingGenerator(definition, generatorType, generatorName);
308293
parameters.putAll( definition.getParameters() );
309294
}
310-
id.setIdentifierGeneratorStrategy( generatorType );
311-
id.setIdentifierGeneratorParameters( parameters );
312-
if ( DEFAULT_ID_GEN_STRATEGY.equals( generatorType ) ) {
313-
id.setNullValue( "undefined" );
295+
else {
296+
generatorStrategy = generatorType;
314297
}
298+
299+
setGeneratorCreator( id, parameters, generatorStrategy );
315300
}
316301

317302
/**
@@ -701,9 +686,8 @@ private static void callConfigure(GeneratorCreationContext creationContext, Gene
701686
Properties parameters = collectParameters(
702687
(SimpleValue) value,
703688
creationContext.getDatabase().getDialect(),
704-
creationContext.getDefaultCatalog(),
705-
creationContext.getDefaultSchema(),
706-
creationContext.getPersistentClass().getRootClass()
689+
creationContext.getPersistentClass().getRootClass(),
690+
emptyMap()
707691
);
708692
( (Configurable) generator ).configure( value.getType(), parameters, creationContext.getServiceRegistry() );
709693
}
@@ -749,51 +733,67 @@ static void createIdGenerator(
749733
}
750734
}
751735

752-
static IdentifierGeneratorDefinition createForeignGenerator(PropertyData mapsIdProperty) {
753-
final IdentifierGeneratorDefinition.Builder foreignGeneratorBuilder =
754-
new IdentifierGeneratorDefinition.Builder();
755-
foreignGeneratorBuilder.setName( "Hibernate-local--foreign generator" );
756-
foreignGeneratorBuilder.setStrategy( "foreign" );
757-
foreignGeneratorBuilder.addParam( "property", mapsIdProperty.getPropertyName() );
758-
return foreignGeneratorBuilder.build();
759-
}
760-
761-
public static void makeIdentifier(
736+
/**
737+
* Set up the identifier generator for an id defined in a {@code hbm.xml} mapping.
738+
*
739+
* @see org.hibernate.boot.model.source.internal.hbm.ModelBinder
740+
*/
741+
public static void makeIdGenerator(
762742
final MappingDocument sourceDocument,
763-
IdentifierGeneratorDefinition generator,
764-
String unsavedValue,
743+
IdentifierGeneratorDefinition definition,
765744
SimpleValue identifierValue,
766745
MetadataBuildingContext buildingContext) {
767-
if ( generator != null ) {
746+
747+
if ( definition != null ) {
768748
final Map<String,Object> params = new HashMap<>();
769749

770750
// see if the specified generator name matches a registered <identifier-generator/>
771-
String generatorName = generator.getStrategy();
772751
final IdentifierGeneratorDefinition generatorDef =
773-
sourceDocument.getMetadataCollector().getIdentifierGenerator( generatorName );
752+
sourceDocument.getMetadataCollector().getIdentifierGenerator( definition.getName() );
753+
final String generatorStrategy;
774754
if ( generatorDef != null ) {
775-
generatorName = generatorDef.getStrategy();
755+
generatorStrategy = generatorDef.getStrategy();
776756
params.putAll( generatorDef.getParameters() );
777757
}
758+
else {
759+
generatorStrategy = definition.getStrategy();
760+
}
778761

779762
// YUCK! but cannot think of a clean way to do this given the string-config based scheme
780763
params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
781764
buildingContext.getObjectNameNormalizer() );
782765

783-
params.putAll( generator.getParameters() );
766+
params.putAll( definition.getParameters() );
784767

785-
identifierValue.setIdentifierGeneratorStrategy( generatorName );
786-
identifierValue.setIdentifierGeneratorParameters( params );
768+
setGeneratorCreator( identifierValue, params, generatorStrategy );
787769
}
770+
}
788771

789-
if ( isNotEmpty( unsavedValue ) ) {
790-
identifierValue.setNullValue( unsavedValue );
791-
}
792-
else if ( DEFAULT_ID_GEN_STRATEGY.equals( identifierValue.getIdentifierGeneratorStrategy() ) ) {
793-
identifierValue.setNullValue( "undefined" );
794-
}
795-
else {
796-
identifierValue.setNullValue( null );
797-
}
772+
private static void setGeneratorCreator(
773+
SimpleValue identifierValue,
774+
Map<String, Object> parameters,
775+
String generatorStrategy) {
776+
identifierValue.setCustomIdGeneratorCreator( new IdentifierGeneratorCreator() {
777+
@Override
778+
public Generator createGenerator(CustomIdGeneratorCreationContext context) {
779+
final Generator generator =
780+
createLegacyIdentifierGenerator(
781+
generatorStrategy,
782+
identifierValue,
783+
context.getDatabase().getDialect(),
784+
context.getRootClass(),
785+
parameters
786+
);
787+
if ( generator instanceof IdentityGenerator) {
788+
identifierValue.setColumnToIdentity();
789+
}
790+
return generator;
791+
}
792+
793+
@Override
794+
public boolean isAssigned() {
795+
return DEFAULT_ID_GEN_STRATEGY.equals( generatorStrategy );
796+
}
797+
} );
798798
}
799799
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagBinder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ else if ( "increment".equals( namedGenerator ) ) {
131131
generatorName = namedGenerator;
132132
}
133133

134-
id.setIdentifierGeneratorStrategy( generatorType );
135-
136134
if ( buildingContext.getBootstrapContext().getJpaCompliance().isGlobalGeneratorScopeEnabled() ) {
137135
SecondPass secondPass = new IdGeneratorResolverSecondPass(
138136
id,

0 commit comments

Comments
 (0)