HHH-15143 - Add an "implicit naming strategy" for database structures (sequence and tables) for identifier generators

HHH-15144 - Add IncubationLogger
This commit is contained in:
Steve Ebersole 2022-03-25 17:51:22 -05:00
parent 117e62195a
commit 33e2faced2
6 changed files with 373 additions and 34 deletions

View File

@ -0,0 +1,49 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.boot.model.naming.spi;
import java.util.Map;
import org.hibernate.Incubating;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.service.ServiceRegistry;
/**
* A naming strategy specifically for determining the implicit naming of
* tables and sequences relating to identifier-generators.
*
* Used in conjunction with
*
* @author Steve Ebersole
*/
@Incubating
public interface ImplicitIdentifierDatabaseObjectNamingStrategy {
/**
* Determine the implicit name for an identifier-generator sequence
*
* @see org.hibernate.id.enhanced.SequenceStyleGenerator
* @see org.hibernate.id.enhanced.SequenceStructure
*/
QualifiedName determineSequenceName(
Identifier catalogName,
Identifier schemaName,
Map<?,?> configValues,
ServiceRegistry serviceRegistry);
/**
* Determine the implicit name for an identifier-generator table
*
* @see org.hibernate.id.enhanced.TableGenerator
* @see org.hibernate.id.enhanced.TableStructure
*/
QualifiedName determineTableName(
Identifier catalogName,
Identifier schemaName,
Map<?,?> configValues,
ServiceRegistry serviceRegistry);
}

View File

@ -7,10 +7,15 @@
package org.hibernate.cfg;
import java.util.function.Supplier;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
import org.hibernate.CustomEntityDirtinessStrategy;
import org.hibernate.Incubating;
import org.hibernate.Interceptor;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.model.naming.spi.ImplicitIdentifierDatabaseObjectNamingStrategy;
import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.jpa.LegacySpecHints;
@ -19,10 +24,6 @@ import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
/**
* Enumerates the configuration properties supported by Hibernate, including
* properties defined by the JPA specification.
@ -775,6 +776,17 @@ public interface AvailableSettings {
*/
String PHYSICAL_NAMING_STRATEGY = "hibernate.physical_naming_strategy";
/**
* An implicit naming-strategy for database structures (tables, sequences) related
* to identifier-generators
*
* @see ImplicitIdentifierDatabaseObjectNamingStrategy
*
* @incubating `ImplicitIdentifierDatabaseObjectNamingStrategy` is considered incubating
*/
@Incubating
String ID_DB_STRUCTURE_NAMING_STRATEGY = "hibernate.id.db_structure_naming_strategy";
/**
* Specifies the order in which metadata sources should be processed, is a delimited list
* of values defined by {@link MetadataSourceType}.

View File

@ -12,10 +12,12 @@ import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.spi.ImplicitIdentifierDatabaseObjectNamingStrategy;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedNameParser;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
@ -34,6 +36,10 @@ import org.hibernate.type.Type;
import org.jboss.logging.Logger;
import static org.hibernate.cfg.AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY;
import static org.hibernate.internal.log.IncubationLogger.INCUBATION_LOGGER;
import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues;
/**
* Generates identifier values based on a sequence-style database structure.
* Variations range from actually using a sequence to using a table to mimic
@ -309,41 +315,41 @@ public class SequenceStyleGenerator
}
// otherwise, determine an implicit name to use
final String implicitName = determineImplicitName( params, jdbcEnv, serviceRegistry );
return new QualifiedNameParser.NameParts(
catalog,
schema,
jdbcEnv.getIdentifierHelper().toIdentifier( implicitName )
);
return determineImplicitName( catalog, schema, params, serviceRegistry );
}
private String determineImplicitName(Properties params, JdbcEnvironment jdbcEnv, ServiceRegistry serviceRegistry) {
final String annotationGeneratorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
final String base = ConfigurationHelper.getString( IMPLICIT_NAME_BASE, params );
final String suffix = ConfigurationHelper.getString( CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, DEF_SEQUENCE_SUFFIX );
private QualifiedName determineImplicitName(
Identifier catalog,
Identifier schema,
Properties params,
ServiceRegistry serviceRegistry) {
final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class );
if ( ! Objects.equals( suffix, DEF_SEQUENCE_SUFFIX ) ) {
// an "implicit name suffix" was specified
if ( StringHelper.isNotEmpty( base ) ) {
if ( Identifier.isQuoted( base ) ) {
return "`" + Identifier.unQuote( base ) + suffix + "`";
}
return base + suffix;
}
}
final String namingStrategySetting = coalesceSuppliedValues(
() -> {
final String localSetting = ConfigurationHelper.getString( ID_DB_STRUCTURE_NAMING_STRATEGY, params );
if ( localSetting != null ) {
INCUBATION_LOGGER.incubatingSetting( ID_DB_STRUCTURE_NAMING_STRATEGY );
}
return localSetting;
},
() -> {
final String globalSetting = ConfigurationHelper.getString( ID_DB_STRUCTURE_NAMING_STRATEGY, configurationService.getSettings() );
if ( globalSetting != null ) {
INCUBATION_LOGGER.incubatingSetting( ID_DB_STRUCTURE_NAMING_STRATEGY );
}
return globalSetting;
},
StandardImplicitIdentifierDatabaseObjectNamingStrategy.class::getName
);
if ( StringHelper.isNotEmpty( annotationGeneratorName ) ) {
return annotationGeneratorName;
}
final ImplicitIdentifierDatabaseObjectNamingStrategy namingStrategy = strategySelector.resolveStrategy(
ImplicitIdentifierDatabaseObjectNamingStrategy.class,
namingStrategySetting
);
if ( StringHelper.isNotEmpty( base ) ) {
if ( Identifier.isQuoted( base ) ) {
return "`" + Identifier.unQuote( base ) + suffix + "`";
}
return base + suffix;
}
throw new MappingException( "Unable to determine sequence name" );
return namingStrategy.determineSequenceName( catalog, schema, params, serviceRegistry );
}
/**

View File

@ -0,0 +1,101 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.id.enhanced;
import java.util.Map;
import java.util.Objects;
import org.hibernate.MappingException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.spi.ImplicitIdentifierDatabaseObjectNamingStrategy;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import static org.hibernate.id.OptimizableGenerator.IMPLICIT_NAME_BASE;
import static org.hibernate.id.enhanced.SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX;
import static org.hibernate.id.enhanced.SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX;
/**
* @author Steve Ebersole
*/
public class StandardImplicitIdentifierDatabaseObjectNamingStrategy implements ImplicitIdentifierDatabaseObjectNamingStrategy {
@Override
public QualifiedName determineSequenceName(
Identifier catalogName,
Identifier schemaName,
Map<?, ?> configValues,
ServiceRegistry serviceRegistry) {
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
final String rootTableName = ConfigurationHelper.getString( PersistentIdentifierGenerator.TABLE, configValues );
final String implicitName = implicitName( rootTableName, configValues, serviceRegistry );
return new QualifiedSequenceName(
catalogName,
schemaName,
jdbcEnvironment.getIdentifierHelper().toIdentifier( implicitName )
);
}
private static String implicitName(
String rootTableName,
Map<?, ?> configValues,
ServiceRegistry serviceRegistry) {
final String annotationGeneratorName = ConfigurationHelper.getString( IdentifierGenerator.GENERATOR_NAME, configValues );
final String base = ConfigurationHelper.getString( IMPLICIT_NAME_BASE, configValues );
final String suffix = ConfigurationHelper.getString( CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, configValues, DEF_SEQUENCE_SUFFIX );
if ( ! Objects.equals( suffix, DEF_SEQUENCE_SUFFIX ) ) {
// an "implicit name suffix" was specified
if ( StringHelper.isNotEmpty( base ) ) {
if ( Identifier.isQuoted( base ) ) {
return "`" + Identifier.unQuote( base ) + suffix + "`";
}
return base + suffix;
}
}
if ( StringHelper.isNotEmpty( annotationGeneratorName ) ) {
return annotationGeneratorName;
}
if ( StringHelper.isNotEmpty( base ) ) {
if ( Identifier.isQuoted( base ) ) {
return "`" + Identifier.unQuote( base ) + suffix + "`";
}
return base + suffix;
}
throw new MappingException( "Unable to determine implicit sequence name; target table - " + rootTableName );
}
@Override
public QualifiedName determineTableName(
Identifier catalogName,
Identifier schemaName,
Map<?, ?> configValues,
ServiceRegistry serviceRegistry) {
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
final String rootTableName = ConfigurationHelper.getString( PersistentIdentifierGenerator.TABLE, configValues );
final String implicitName = implicitName( rootTableName, configValues, serviceRegistry );
return new QualifiedTableName(
catalogName,
schemaName,
jdbcEnvironment.getIdentifierHelper().toIdentifier( implicitName )
);
}
}

View File

@ -0,0 +1,34 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.internal.log;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;
import org.jboss.logging.annotations.ValidIdRange;
import static org.jboss.logging.Logger.Level.WARN;
/**
* @author Steve Ebersole
*/
@MessageLogger(projectCode = "HHH")
@ValidIdRange(min = 90006001, max = 90007000)
public interface IncubationLogger {
String CATEGORY = SubSystemLogging.BASE + ".deprecation";
IncubationLogger INCUBATION_LOGGER = Logger.getMessageLogger( IncubationLogger.class, CATEGORY );
@LogMessage(level = WARN)
@Message(
id = 90006001,
value = "Encountered incubating setting [%s]. See javadoc on corresponding " +
"`org.hibernate.cfg.AvailableSettings` constant for details."
)
void incubatingSetting(String settingName);
}

View File

@ -0,0 +1,137 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.namingstrategy;
import java.util.Map;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.spi.ImplicitIdentifierDatabaseObjectNamingStrategy;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.DomainModelScope;
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Steve Ebersole
*/
@org.hibernate.testing.orm.junit.ServiceRegistry(
settings = @Setting(
name = AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY,
value = "org.hibernate.orm.test.namingstrategy.ImplicitIdentifierDatabaseObjectNamingStrategyTests$Strategy"
)
)
@DomainModel( annotatedClasses = ImplicitIdentifierDatabaseObjectNamingStrategyTests.TheEntity.class )
public class ImplicitIdentifierDatabaseObjectNamingStrategyTests {
@Test
public void testIt(DomainModelScope domainModelScope, ServiceRegistryScope serviceRegistryScope) {
domainModelScope.withHierarchy( TheEntity.class, (entityDescriptor) -> {
final IdentifierGeneratorFactory identifierGeneratorFactory = domainModelScope.getDomainModel()
.getMetadataBuildingOptions()
.getIdentifierGeneratorFactory();
final JdbcServices jdbcServices = serviceRegistryScope.getRegistry().getService( JdbcServices.class );
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SequenceStyleGenerator generator = (SequenceStyleGenerator) entityDescriptor.getIdentifier().createIdentifierGenerator(
identifierGeneratorFactory,
jdbcEnvironment.getDialect(),
entityDescriptor
);
final String sequenceName = generator.getDatabaseStructure().getPhysicalName().getObjectName().getText();
assertThat( sequenceName ).isEqualTo( "ents_ids_seq" );
} );
}
public static class Strategy implements ImplicitIdentifierDatabaseObjectNamingStrategy {
@Override
public QualifiedName determineSequenceName(
Identifier catalogName,
Identifier schemaName,
Map<?,?> configValues,
ServiceRegistry serviceRegistry) {
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
final String rootTableName = ConfigurationHelper.getString( PersistentIdentifierGenerator.TABLE, configValues );
final String structureName = String.format( "%s_ids_seq", rootTableName );
return new QualifiedSequenceName(
catalogName,
schemaName,
jdbcEnvironment.getIdentifierHelper().toIdentifier( structureName )
);
}
@Override
public QualifiedName determineTableName(
Identifier catalogName,
Identifier schemaName,
Map<?,?> configValues,
ServiceRegistry serviceRegistry) {
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
final String rootTableName = ConfigurationHelper.getString( PersistentIdentifierGenerator.TABLE, configValues );
final String structureName = String.format( "%s_ids_tbl", rootTableName );
return new QualifiedSequenceName(
catalogName,
schemaName,
jdbcEnvironment.getIdentifierHelper().toIdentifier( structureName )
);
}
}
@Entity( name = "TheEntity" )
@Table( name = "ents" )
public static class TheEntity {
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private Integer id;
@Basic
private String name;
private TheEntity() {
// for use by Hibernate
}
public TheEntity(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}