Skip to content

Commit

Permalink
HHH-18337 Account for physical naming strategy when querying db sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel authored and sebersole committed Sep 16, 2024
1 parent 7c66788 commit 88bcc71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.PropertyData;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
Expand Down Expand Up @@ -1441,6 +1443,14 @@ static void processId(
? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer()
: null;
idValue.setCustomIdGeneratorCreator( identifierGeneratorCreator( idProperty, annotation, beanContainer ) );
final Map<String,Object> parameters = new HashMap<>();
parameters.put( PersistentIdentifierGenerator.TABLE, idValue.getTable().getName() );
if ( idValue.getColumnSpan() == 1 ) {
parameters.put( PersistentIdentifierGenerator.PK, idValue.getColumns().get(0).getName() );
}
// YUCK! but cannot think of a clean way to do this given the string-config based scheme
parameters.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, context.getObjectNameNormalizer() );
idValue.setIdentifierGeneratorParameters( parameters );
}
else if ( !generatorAnnotations.isEmpty() ) {
// idValue.setCustomGeneratorCreator( generatorCreator( idProperty, generatorAnnotation ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Identifier normalizeIdentifierQuoting(String identifierText) {
return database().toIdentifier( identifierText );
}

protected Database database() {
public Database database() {
if ( database == null ) {
database = getBuildingContext().getMetadataCollector().getDatabase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ObjectNameNormalizer;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedNameParser;
Expand Down Expand Up @@ -216,7 +217,8 @@ public void configure(Type type, Properties parameters, ServiceRegistry serviceR
physicalSequence,
optimizationStrategy,
serviceRegistry,
determineContributor( parameters )
determineContributor( parameters ),
(ObjectNameNormalizer) parameters.get( IDENTIFIER_NORMALIZER )
);

if ( physicalSequence
Expand Down Expand Up @@ -251,7 +253,8 @@ private int adjustIncrementSize(
boolean physicalSequence,
OptimizerDescriptor optimizationStrategy,
ServiceRegistry serviceRegistry,
String contributor) {
String contributor,
ObjectNameNormalizer normalizer) {
final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class );
final SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting(
AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY,
Expand All @@ -262,7 +265,10 @@ private int adjustIncrementSize(
if ( sequenceMismatchStrategy != SequenceMismatchStrategy.NONE
&& optimizationStrategy.isPooled()
&& physicalSequence ) {
final String databaseSequenceName = sequenceName.getObjectName().getText();
final String databaseSequenceName = normalizer.database()
.getPhysicalNamingStrategy()
.toPhysicalSequenceName( sequenceName.getObjectName(), jdbcEnvironment )
.getText();
final Number databaseIncrementValue = isSchemaToBeRecreated( contributor, configurationService ) ? null : getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );
if ( databaseIncrementValue != null && databaseIncrementValue.intValue() != incrementSize) {
final int dbIncrementValue = databaseIncrementValue.intValue();
Expand Down

0 comments on commit 88bcc71

Please sign in to comment.