extract a method

This commit is contained in:
Gavin King 2024-03-21 15:23:31 +01:00
parent 9362964ebd
commit c14c608199
1 changed files with 63 additions and 56 deletions

View File

@ -10,7 +10,6 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.OptimizableGenerator; import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.PersistentIdentifierGenerator;
@ -23,6 +22,7 @@ import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table; import org.hibernate.mapping.Table;
import org.hibernate.generator.Generator; import org.hibernate.generator.Generator;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
public class IdentifierGeneratorUtil { public class IdentifierGeneratorUtil {
@ -34,72 +34,62 @@ public class IdentifierGeneratorUtil {
String defaultCatalog, String defaultCatalog,
String defaultSchema, String defaultSchema,
RootClass rootClass) { RootClass rootClass) {
return identifierGeneratorFactory.createIdentifierGenerator(
simpleValue.getIdentifierGeneratorStrategy(),
simpleValue.getType(),
collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass )
);
}
static Properties collectParameters(
SimpleValue simpleValue, Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) {
final ConfigurationService configService =
simpleValue.getMetadata().getMetadataBuildingOptions().getServiceRegistry()
.requireService( ConfigurationService.class );
final Properties params = new Properties(); final Properties params = new Properties();
// This is for backwards compatibility only; // This is for backwards compatibility only;
// when this method is called by Hibernate ORM, defaultSchema and defaultCatalog are always // when this method is called by Hibernate ORM, defaultSchema and defaultCatalog are always
// null, and defaults are handled later. // null, and defaults are handled later.
if ( defaultSchema != null ) { if ( defaultSchema != null ) {
params.setProperty( PersistentIdentifierGenerator.SCHEMA, defaultSchema ); params.setProperty( PersistentIdentifierGenerator.SCHEMA, defaultSchema);
} }
if ( defaultCatalog != null ) { if ( defaultCatalog != null ) {
params.setProperty( PersistentIdentifierGenerator.CATALOG, defaultCatalog ); params.setProperty( PersistentIdentifierGenerator.CATALOG, defaultCatalog);
} }
// default initial value and allocation size per-JPA defaults // default initial value and allocation size per-JPA defaults
params.setProperty( OptimizableGenerator.INITIAL_PARAM, String.valueOf( OptimizableGenerator.DEFAULT_INITIAL_VALUE ) ); params.setProperty( OptimizableGenerator.INITIAL_PARAM,
final ConfigurationService cs = simpleValue.getMetadata().getMetadataBuildingOptions().getServiceRegistry() String.valueOf( OptimizableGenerator.DEFAULT_INITIAL_VALUE ) );
.requireService( ConfigurationService.class );
final String idNamingStrategy = cs.getSetting( params.setProperty( OptimizableGenerator.INCREMENT_PARAM,
AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY, String.valueOf( defaultIncrement( configService ) ) );
StandardConverters.STRING,
null
);
if ( LegacyNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| LegacyNamingStrategy.class.getName().equals( idNamingStrategy )
|| SingleNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| SingleNamingStrategy.class.getName().equals( idNamingStrategy ) ) {
params.setProperty( OptimizableGenerator.INCREMENT_PARAM, "1" );
}
else {
params.setProperty(
OptimizableGenerator.INCREMENT_PARAM,
String.valueOf( OptimizableGenerator.DEFAULT_INCREMENT_SIZE )
);
}
//init the table here instead of earlier, so that we can get a quoted table name //init the table here instead of earlier, so that we can get a quoted table name
//TODO: would it be better to simply pass the qualified table name, instead of //TODO: would it be better to simply pass the qualified table name, instead of
// splitting it up into schema/catalog/table names // splitting it up into schema/catalog/table names
final String tableName = simpleValue.getTable().getQuotedName(dialect); final String tableName = simpleValue.getTable().getQuotedName( dialect );
params.setProperty( PersistentIdentifierGenerator.TABLE, tableName ); params.setProperty( PersistentIdentifierGenerator.TABLE, tableName );
//pass the column name (a generated id almost always has a single column) //pass the column name (a generated id almost always has a single column)
final String columnName = ( (Column) simpleValue.getSelectables().get(0) ).getQuotedName(dialect); final Column column = (Column) simpleValue.getSelectables().get(0);
final String columnName = column.getQuotedName( dialect );
params.setProperty( PersistentIdentifierGenerator.PK, columnName ); params.setProperty( PersistentIdentifierGenerator.PK, columnName );
//pass the entity-name, if not a collection-id //pass the entity-name, if not a collection-id
if ( rootClass != null ) { if ( rootClass != null ) {
params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() ); params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() );
params.setProperty( IdentifierGenerator.JPA_ENTITY_NAME, rootClass.getJpaEntityName() ); params.setProperty( IdentifierGenerator.JPA_ENTITY_NAME, rootClass.getJpaEntityName() );
// The table name is not really a good default for subselect entities, so use the JPA entity name which is short // The table name is not really a good default for subselect entities,
if ( simpleValue.getTable().isSubselect() ) { // so use the JPA entity name which is short
params.setProperty( OptimizableGenerator.IMPLICIT_NAME_BASE, rootClass.getJpaEntityName() ); params.setProperty( OptimizableGenerator.IMPLICIT_NAME_BASE,
} simpleValue.getTable().isSubselect()
else { ? rootClass.getJpaEntityName()
params.setProperty( OptimizableGenerator.IMPLICIT_NAME_BASE, simpleValue.getTable().getName() ); : simpleValue.getTable().getName() );
}
final StringBuilder tables = new StringBuilder(); params.setProperty( PersistentIdentifierGenerator.TABLES,
for ( Table table : rootClass.getIdentityTables() ) { identityTablesString( dialect, rootClass ) );
tables.append( table.getQuotedName(dialect) );
if ( tables.length()>0 ) {
tables.append( ", " );
}
}
params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() );
} }
else { else {
params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); params.setProperty( PersistentIdentifierGenerator.TABLES, tableName );
@ -112,25 +102,42 @@ public class IdentifierGeneratorUtil {
// TODO : we should pass along all settings once "config lifecycle" is hashed out... // TODO : we should pass along all settings once "config lifecycle" is hashed out...
params.put( params.put( IdentifierGenerator.CONTRIBUTOR_NAME,
IdentifierGenerator.CONTRIBUTOR_NAME, simpleValue.getBuildingContext().getCurrentContributorName() );
simpleValue.getBuildingContext().getCurrentContributorName()
);
if ( cs.getSettings().get( AvailableSettings.PREFERRED_POOLED_OPTIMIZER ) != null ) { final Map<String, Object> settings = configService.getSettings();
params.put( if ( settings.containsKey( AvailableSettings.PREFERRED_POOLED_OPTIMIZER ) ) {
AvailableSettings.PREFERRED_POOLED_OPTIMIZER, params.put( AvailableSettings.PREFERRED_POOLED_OPTIMIZER,
cs.getSettings().get( AvailableSettings.PREFERRED_POOLED_OPTIMIZER ) settings.get( AvailableSettings.PREFERRED_POOLED_OPTIMIZER ) );
);
} }
final Generator generator = identifierGeneratorFactory.createIdentifierGenerator( return params;
simpleValue.getIdentifierGeneratorStrategy(), }
simpleValue.getType(),
params
);
return generator; private static String identityTablesString(Dialect dialect, RootClass rootClass) {
final StringBuilder tables = new StringBuilder();
for ( Table table : rootClass.getIdentityTables() ) {
tables.append( table.getQuotedName( dialect ) );
if ( tables.length()>0 ) {
tables.append( ", " );
}
}
return tables.toString();
}
private static int defaultIncrement(ConfigurationService configService) {
final String idNamingStrategy =
configService.getSetting( AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY,
StandardConverters.STRING, null );
if ( LegacyNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| LegacyNamingStrategy.class.getName().equals( idNamingStrategy )
|| SingleNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| SingleNamingStrategy.class.getName().equals( idNamingStrategy ) ) {
return 1;
}
else {
return OptimizableGenerator.DEFAULT_INCREMENT_SIZE;
}
} }
} }