HHH-18625 - Add Configurable#create(GeneratorCreationContext)

https://hibernate.atlassian.net/browse/HHH-18625
This commit is contained in:
Gavin King 2024-09-11 19:42:14 +02:00 committed by Steve Ebersole
parent a1d0b82c8a
commit b09b27c7d6
7 changed files with 79 additions and 17 deletions

View File

@ -529,14 +529,20 @@ public class GeneratorBinder {
private static void callConfigure(GeneratorCreationContext creationContext, Generator generator) {
if ( generator instanceof Configurable ) {
final Configurable configurable = (Configurable) generator;
final Value value = creationContext.getProperty().getValue();
( (Configurable) generator ).configure( value.getType(), collectParameters(
(SimpleValue) value,
creationContext.getDatabase().getDialect(),
creationContext.getDefaultCatalog(),
creationContext.getDefaultSchema(),
creationContext.getPersistentClass().getRootClass()
), creationContext.getServiceRegistry() );
configurable.create( creationContext );
configurable.configure(
value.getType(),
collectParameters(
(SimpleValue) value,
creationContext.getDatabase().getDialect(),
creationContext.getDefaultCatalog(),
creationContext.getDefaultSchema(),
creationContext.getPersistentClass().getRootClass()
),
creationContext.getServiceRegistry()
);
}
}

View File

@ -42,10 +42,7 @@ public abstract class ObjectNameNormalizer {
}
public Identifier normalizeIdentifierQuoting(Identifier identifier) {
return getBuildingContext().getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
return database().getJdbcEnvironment().getIdentifierHelper()
.normalizeQuoting( identifier );
}

View File

@ -19,7 +19,6 @@ import org.hibernate.dialect.DatabaseVersion;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.batch.spi.BatchBuilder;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;

View File

@ -11,6 +11,7 @@ import java.util.Properties;
import org.hibernate.MappingException;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
@ -21,6 +22,14 @@ import org.hibernate.type.Type;
* @author Steve Ebersole
*/
public interface Configurable {
/**
* Called before {@link #configure(Type, Properties, ServiceRegistry)},
* with an instance of {@link GeneratorCreationContext}.
*
* @since 6.6
*/
default void create(GeneratorCreationContext creationContext) throws MappingException {}
/**
* Configure this instance, given the value of parameters
* specified by the user as XML {@code <param>} elements and

View File

@ -9,7 +9,7 @@ package org.hibernate.id.factory;
import java.util.Properties;
import org.hibernate.Incubating;
import org.hibernate.dialect.Dialect;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.spi.GeneratorDefinitionResolver;
import org.hibernate.service.Service;
@ -71,5 +71,21 @@ public interface IdentifierGeneratorFactory extends Service {
* @deprecated use {@link #createIdentifierGenerator(GenerationType, String, String, JavaType, Properties, GeneratorDefinitionResolver)}
*/
@Deprecated(since = "6.0")
Generator createIdentifierGenerator(String strategy, Type type, Properties parameters);
Generator createIdentifierGenerator(String strategy, Type type, GeneratorCreationContext creationContext, Properties parameters);
/**
* Given a strategy, retrieve the appropriate identifier generator instance.
*
* @param strategy The generation strategy.
* @param type The mapping type for the identifier values.
* @param parameters Any parameters properties given in the generator mapping.
*
* @return The appropriate generator instance.
*
* @deprecated use {@link #createIdentifierGenerator(GenerationType, String, String, JavaType, Properties, GeneratorDefinitionResolver)}
*/
@Deprecated(since = "6.0")
default Generator createIdentifierGenerator(String strategy, Type type, Properties parameters) {
return createIdentifierGenerator( strategy, type, null, parameters );
}
}

View File

@ -6,10 +6,12 @@
*/
package org.hibernate.id.factory.internal;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
@ -17,10 +19,13 @@ import org.hibernate.id.enhanced.LegacyNamingStrategy;
import org.hibernate.id.enhanced.SingleNamingStrategy;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.generator.Generator;
import org.hibernate.service.ServiceRegistry;
import java.util.Map;
import java.util.Properties;
@ -37,6 +42,32 @@ public class IdentifierGeneratorUtil {
return identifierGeneratorFactory.createIdentifierGenerator(
simpleValue.getIdentifierGeneratorStrategy(),
simpleValue.getType(),
new GeneratorCreationContext() {
@Override
public Database getDatabase() {
return simpleValue.getMetadata().getDatabase();
}
@Override
public ServiceRegistry getServiceRegistry() {
return simpleValue.getServiceRegistry();
}
@Override
public String getDefaultCatalog() {
return null;
}
@Override
public String getDefaultSchema() {
return null;
}
@Override
public PersistentClass getPersistentClass() {
return rootClass;
}
@Override
public Property getProperty() {
return rootClass.getIdentifierProperty();
}
},
collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass )
);
}
@ -48,7 +79,7 @@ public class IdentifierGeneratorUtil {
String defaultSchema,
RootClass rootClass) {
final ConfigurationService configService =
simpleValue.getMetadata().getMetadataBuildingOptions().getServiceRegistry()
simpleValue.getServiceRegistry()
.requireService( ConfigurationService.class );
final Properties params = new Properties();

View File

@ -19,6 +19,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.generator.Generator;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.Assigned;
import org.hibernate.id.Configurable;
import org.hibernate.id.ForeignGenerator;
@ -206,7 +207,8 @@ public class StandardIdentifierGeneratorFactory
}
@Override @Deprecated
public Generator createIdentifierGenerator(String strategy, Type type, Properties parameters) {
public Generator createIdentifierGenerator(
String strategy, Type type, GeneratorCreationContext creationContext, Properties parameters) {
try {
final Class<? extends Generator> clazz = getIdentifierGeneratorClass( strategy );
final Generator identifierGenerator;
@ -222,7 +224,9 @@ public class StandardIdentifierGeneratorFactory
}
if ( identifierGenerator instanceof Configurable ) {
( (Configurable) identifierGenerator ).configure( type, parameters, serviceRegistry );
final Configurable configurable = (Configurable) identifierGenerator;
configurable.create( creationContext );
configurable.configure( type, parameters, serviceRegistry );
}
return identifierGenerator;
}