Skip to content

Commit 723e18d

Browse files
committed
HHH-18337 - SequenceStyleGenerator not respecting physical naming strategy
1 parent 88bcc71 commit 723e18d

File tree

5 files changed

+126
-20
lines changed

5 files changed

+126
-20
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,14 +1443,6 @@ static void processId(
14431443
? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer()
14441444
: null;
14451445
idValue.setCustomIdGeneratorCreator( identifierGeneratorCreator( idProperty, annotation, beanContainer ) );
1446-
final Map<String,Object> parameters = new HashMap<>();
1447-
parameters.put( PersistentIdentifierGenerator.TABLE, idValue.getTable().getName() );
1448-
if ( idValue.getColumnSpan() == 1 ) {
1449-
parameters.put( PersistentIdentifierGenerator.PK, idValue.getColumns().get(0).getName() );
1450-
}
1451-
// YUCK! but cannot think of a clean way to do this given the string-config based scheme
1452-
parameters.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, context.getObjectNameNormalizer() );
1453-
idValue.setIdentifierGeneratorParameters( parameters );
14541446
}
14551447
else if ( !generatorAnnotations.isEmpty() ) {
14561448
// idValue.setCustomGeneratorCreator( generatorCreator( idProperty, generatorAnnotation ) );

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,41 @@
1212
import org.hibernate.mapping.Property;
1313
import org.hibernate.service.ServiceRegistry;
1414

15+
/**
16+
* Access to information useful during {@linkplain Generator} creation and initialization.
17+
*
18+
* @see AnnotationBasedGenerator
19+
* @see org.hibernate.id.Configurable#create(GeneratorCreationContext)
20+
*/
1521
@Incubating
1622
public interface GeneratorCreationContext {
23+
/**
24+
* View of the relational database objects (tables, sequences, ...) and namespaces (catalogs and schemas).
25+
*/
1726
Database getDatabase();
27+
28+
/**
29+
* Access to available services.
30+
*/
1831
ServiceRegistry getServiceRegistry();
1932

33+
/**
34+
* The default catalog name, if one.
35+
*/
2036
String getDefaultCatalog();
37+
38+
/**
39+
* The default schema name, if one.
40+
*/
2141
String getDefaultSchema();
2242

43+
/**
44+
* Mapping details for the entity; may be null in the case of and id-bag id generator.
45+
*/
2346
PersistentClass getPersistentClass();
2447

48+
/**
49+
* The entity identifier or id-bag property details.
50+
*/
2551
Property getProperty();
2652
}

hibernate-core/src/main/java/org/hibernate/id/Configurable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Properties;
1010

11+
import org.hibernate.Incubating;
1112
import org.hibernate.MappingException;
1213
import org.hibernate.boot.model.relational.Database;
1314
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
@@ -18,6 +19,11 @@
1819
/**
1920
* A {@link org.hibernate.generator.Generator} that supports "configuration".
2021
*
22+
* @apiNote Prefer instead using either<ul>
23+
* <li>{@linkplain org.hibernate.annotations.IdGeneratorType}</li>, or
24+
* <li>{@linkplain org.hibernate.generator.AnnotationBasedGenerator} (in either implementation or constructor-injection form)</li>
25+
* </ul>
26+
*
2127
* @author Gavin King
2228
* @author Steve Ebersole
2329
*/
@@ -27,7 +33,11 @@ public interface Configurable {
2733
* with an instance of {@link GeneratorCreationContext}.
2834
*
2935
* @since 6.6
36+
* @deprecated Added in 6.6 as a short-term work-around, but should really use on
37+
* of the alternatives discussed at {@linkplain Configurable}.
38+
* See <a href="https://hibernate.atlassian.net/browse/HHH-18615">HHH-18615</a>.
3039
*/
40+
@Deprecated(forRemoval = true)
3141
default void create(GeneratorCreationContext creationContext) throws MappingException {}
3242

3343
/**

hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.MappingException;
1717
import org.hibernate.boot.model.naming.Identifier;
1818
import org.hibernate.boot.model.naming.ObjectNameNormalizer;
19+
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
1920
import org.hibernate.boot.model.relational.Database;
2021
import org.hibernate.boot.model.relational.QualifiedName;
2122
import org.hibernate.boot.model.relational.QualifiedNameParser;
@@ -27,6 +28,7 @@
2728
import org.hibernate.engine.config.spi.ConfigurationService;
2829
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
2930
import org.hibernate.engine.spi.SharedSessionContractImplementor;
31+
import org.hibernate.generator.GeneratorCreationContext;
3032
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
3133
import org.hibernate.id.IdentifierGenerator;
3234
import org.hibernate.id.PersistentIdentifierGenerator;
@@ -164,6 +166,8 @@ public class SequenceStyleGenerator
164166
private Optimizer optimizer;
165167
private Type identifierType;
166168

169+
private PhysicalNamingStrategy physicalNamingStrategy;
170+
167171
/**
168172
* Getter for property 'databaseStructure'.
169173
*
@@ -195,8 +199,17 @@ public Type getIdentifierType() {
195199

196200
// Configurable implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197201

202+
@Override
203+
public void create(GeneratorCreationContext creationContext) throws MappingException {
204+
physicalNamingStrategy = creationContext.getDatabase().getPhysicalNamingStrategy();
205+
}
206+
198207
@Override
199208
public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) throws MappingException {
209+
if ( physicalNamingStrategy == null ) {
210+
throw new IllegalStateException( "Expecting prior call to #create" );
211+
}
212+
200213
final JdbcEnvironment jdbcEnvironment = serviceRegistry.requireService( JdbcEnvironment.class );
201214
final Dialect dialect = jdbcEnvironment.getDialect();
202215

@@ -217,8 +230,7 @@ public void configure(Type type, Properties parameters, ServiceRegistry serviceR
217230
physicalSequence,
218231
optimizationStrategy,
219232
serviceRegistry,
220-
determineContributor( parameters ),
221-
(ObjectNameNormalizer) parameters.get( IDENTIFIER_NORMALIZER )
233+
determineContributor( parameters )
222234
);
223235

224236
if ( physicalSequence
@@ -244,6 +256,9 @@ public void configure(Type type, Properties parameters, ServiceRegistry serviceR
244256
getInt( INITIAL_PARAM, parameters, -1 )
245257
);
246258
this.databaseStructure.configure( optimizer );
259+
260+
// we don't want or need this after initialization is complete
261+
physicalNamingStrategy = null;
247262
}
248263

249264
private int adjustIncrementSize(
@@ -253,8 +268,7 @@ private int adjustIncrementSize(
253268
boolean physicalSequence,
254269
OptimizerDescriptor optimizationStrategy,
255270
ServiceRegistry serviceRegistry,
256-
String contributor,
257-
ObjectNameNormalizer normalizer) {
271+
String contributor) {
258272
final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class );
259273
final SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting(
260274
AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY,
@@ -265,8 +279,7 @@ private int adjustIncrementSize(
265279
if ( sequenceMismatchStrategy != SequenceMismatchStrategy.NONE
266280
&& optimizationStrategy.isPooled()
267281
&& physicalSequence ) {
268-
final String databaseSequenceName = normalizer.database()
269-
.getPhysicalNamingStrategy()
282+
final String databaseSequenceName = physicalNamingStrategy
270283
.toPhysicalSequenceName( sequenceName.getObjectName(), jdbcEnvironment )
271284
.getText();
272285
final Number databaseIncrementValue = isSchemaToBeRecreated( contributor, configurationService ) ? null : getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );

hibernate-core/src/test/java/org/hibernate/orm/test/id/enhanced/SequenceStyleConfigUnitTest.java

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.dialect.sequence.ANSISequenceSupport;
2121
import org.hibernate.dialect.sequence.SequenceSupport;
2222
import org.hibernate.engine.jdbc.spi.JdbcServices;
23+
import org.hibernate.generator.GeneratorCreationContext;
2324
import org.hibernate.id.OptimizableGenerator;
2425
import org.hibernate.id.PersistentIdentifierGenerator;
2526
import org.hibernate.id.enhanced.DatabaseStructure;
@@ -33,6 +34,9 @@
3334
import org.hibernate.id.enhanced.SequenceStyleGenerator;
3435
import org.hibernate.id.enhanced.StandardOptimizerDescriptor;
3536
import org.hibernate.id.enhanced.TableStructure;
37+
import org.hibernate.mapping.PersistentClass;
38+
import org.hibernate.mapping.Property;
39+
import org.hibernate.service.ServiceRegistry;
3640
import org.hibernate.type.StandardBasicTypes;
3741
import org.hibernate.type.spi.TypeConfiguration;
3842

@@ -70,16 +74,18 @@ public void testDefaultedSequenceBackedConfiguration() {
7074
() -> buildingContext.getBootstrapContext().getTypeConfiguration(),
7175
serviceRegistry
7276
);
77+
Database database = new Database( buildingContext.getBuildingOptions() );
78+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
7379
Properties props = buildGeneratorPropertiesBase( buildingContext );
7480
SequenceStyleGenerator generator = new SequenceStyleGenerator();
81+
generator.create( generatorCreationContext );
7582
generator.configure(
7683
new TypeConfiguration().getBasicTypeRegistry()
7784
.resolve( StandardBasicTypes.LONG ),
7885
props,
7986
serviceRegistry
8087
);
8188

82-
Database database = new Database( buildingContext.getBuildingOptions() );
8389
generator.registerExportables( database );
8490
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
8591

@@ -160,18 +166,22 @@ public void testDefaultOptimizerBasedOnIncrementBackedBySequence() {
160166
() -> buildingContext.getBootstrapContext().getTypeConfiguration(),
161167
serviceRegistry
162168
);
169+
170+
Database database = new Database( buildingContext.getBuildingOptions() );
171+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
172+
163173
Properties props = buildGeneratorPropertiesBase( buildingContext );
164174
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "10" );
165175

166176
SequenceStyleGenerator generator = new SequenceStyleGenerator();
177+
generator.create( generatorCreationContext );
167178
generator.configure(
168179
new TypeConfiguration().getBasicTypeRegistry()
169180
.resolve( StandardBasicTypes.LONG ),
170181
props,
171182
serviceRegistry
172183
);
173184

174-
Database database = new Database( buildingContext.getBuildingOptions() );
175185
generator.registerExportables( database );
176186
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
177187

@@ -190,17 +200,20 @@ public void testDefaultOptimizerBasedOnIncrementBackedBySequence() {
190200
() -> buildingContext.getBootstrapContext().getTypeConfiguration(),
191201
serviceRegistry
192202
);
203+
Database database = new Database( buildingContext.getBuildingOptions() );
204+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
205+
193206
Properties props = buildGeneratorPropertiesBase( buildingContext );
194207
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "10" );
195208

196209
SequenceStyleGenerator generator = new SequenceStyleGenerator();
210+
generator.create( generatorCreationContext );
197211
generator.configure(
198212
new TypeConfiguration().getBasicTypeRegistry()
199213
.resolve( StandardBasicTypes.LONG ),
200214
props,
201215
serviceRegistry
202216
);
203-
Database database = new Database( buildingContext.getBuildingOptions() );
204217
generator.registerExportables( database );
205218
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
206219

@@ -226,17 +239,20 @@ public void testDefaultOptimizerBasedOnIncrementBackedByTable() {
226239
() -> buildingContext.getBootstrapContext().getTypeConfiguration(),
227240
serviceRegistry
228241
);
242+
Database database = new Database( buildingContext.getBuildingOptions() );
243+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
244+
229245
Properties props = buildGeneratorPropertiesBase( buildingContext );
230246
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "10" );
231247

232248
SequenceStyleGenerator generator = new SequenceStyleGenerator();
249+
generator.create( generatorCreationContext );
233250
generator.configure(
234251
new TypeConfiguration().getBasicTypeRegistry()
235252
.resolve( StandardBasicTypes.LONG ),
236253
props,
237254
serviceRegistry
238255
);
239-
Database database = new Database( buildingContext.getBuildingOptions() );
240256
generator.registerExportables( database );
241257
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
242258

@@ -285,6 +301,44 @@ public void testForceTableUse() {
285301
}
286302
}
287303

304+
private static class GeneratorCreationContextImpl implements GeneratorCreationContext {
305+
private final Database database;
306+
307+
public GeneratorCreationContextImpl(Database database) {
308+
this.database = database;
309+
}
310+
311+
@Override
312+
public Database getDatabase() {
313+
return database;
314+
}
315+
316+
@Override
317+
public ServiceRegistry getServiceRegistry() {
318+
return database.getServiceRegistry();
319+
}
320+
321+
@Override
322+
public String getDefaultCatalog() {
323+
throw new UnsupportedOperationException();
324+
}
325+
326+
@Override
327+
public String getDefaultSchema() {
328+
throw new UnsupportedOperationException();
329+
}
330+
331+
@Override
332+
public PersistentClass getPersistentClass() {
333+
throw new UnsupportedOperationException();
334+
}
335+
336+
@Override
337+
public Property getProperty() {
338+
throw new UnsupportedOperationException();
339+
}
340+
}
341+
288342
/**
289343
* Test explicitly specifying both optimizer and increment
290344
*/
@@ -302,14 +356,18 @@ public void testExplicitOptimizerWithExplicitIncrementSize() {
302356
Properties props = buildGeneratorPropertiesBase( buildingContext );
303357
props.setProperty( SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.NONE.getExternalName() );
304358
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "20" );
359+
360+
Database database = new Database( buildingContext.getBuildingOptions() );
361+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
362+
305363
SequenceStyleGenerator generator = new SequenceStyleGenerator();
364+
generator.create( generatorCreationContext );
306365
generator.configure(
307366
new TypeConfiguration().getBasicTypeRegistry()
308367
.resolve( StandardBasicTypes.LONG ),
309368
props,
310369
serviceRegistry
311370
);
312-
Database database = new Database( buildingContext.getBuildingOptions() );
313371
generator.registerExportables( database );
314372
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
315373

@@ -323,6 +381,7 @@ public void testExplicitOptimizerWithExplicitIncrementSize() {
323381
props.setProperty( SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.HILO.getExternalName() );
324382
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "20" );
325383
generator = new SequenceStyleGenerator();
384+
generator.create( generatorCreationContext );
326385
generator.configure(
327386
new TypeConfiguration().getBasicTypeRegistry()
328387
.resolve( StandardBasicTypes.LONG ),
@@ -341,6 +400,7 @@ public void testExplicitOptimizerWithExplicitIncrementSize() {
341400
props.setProperty( SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName() );
342401
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "20" );
343402
generator = new SequenceStyleGenerator();
403+
generator.create( generatorCreationContext );
344404
generator.configure(
345405
new TypeConfiguration().getBasicTypeRegistry()
346406
.resolve( StandardBasicTypes.LONG ),
@@ -368,23 +428,27 @@ public void testPreferredPooledOptimizerSetting() {
368428
() -> buildingContext.getBootstrapContext().getTypeConfiguration(),
369429
serviceRegistry
370430
);
431+
Database database = new Database( buildingContext.getBuildingOptions() );
432+
GeneratorCreationContextImpl generatorCreationContext = new GeneratorCreationContextImpl( database );
433+
371434
Properties props = buildGeneratorPropertiesBase( buildingContext );
372435
props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "20" );
373436
SequenceStyleGenerator generator = new SequenceStyleGenerator();
437+
generator.create( generatorCreationContext );
374438
generator.configure(
375439
new TypeConfiguration().getBasicTypeRegistry()
376440
.resolve( StandardBasicTypes.LONG ),
377441
props,
378442
serviceRegistry
379443
);
380-
Database database = new Database( buildingContext.getBuildingOptions() );
381444
generator.registerExportables( database );
382445
generator.initialize( SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ) );
383446
assertClassAssignability( SequenceStructure.class, generator.getDatabaseStructure().getClass() );
384447
assertClassAssignability( PooledOptimizer.class, generator.getOptimizer().getClass() );
385448

386449
props.setProperty( Environment.PREFERRED_POOLED_OPTIMIZER, StandardOptimizerDescriptor.POOLED_LO.getExternalName() );
387450
generator = new SequenceStyleGenerator();
451+
generator.create( generatorCreationContext );
388452
generator.configure(
389453
new TypeConfiguration().getBasicTypeRegistry()
390454
.resolve( StandardBasicTypes.LONG ),
@@ -398,6 +462,7 @@ public void testPreferredPooledOptimizerSetting() {
398462

399463
props.setProperty( Environment.PREFERRED_POOLED_OPTIMIZER, StandardOptimizerDescriptor.POOLED_LOTL.getExternalName() );
400464
generator = new SequenceStyleGenerator();
465+
generator.create( generatorCreationContext );
401466
generator.configure(
402467
new TypeConfiguration().getBasicTypeRegistry()
403468
.resolve( StandardBasicTypes.LONG ),

0 commit comments

Comments
 (0)