some misc minor cleanups

This commit is contained in:
Gavin 2022-12-19 01:07:55 +01:00 committed by Gavin King
parent 782d2c9707
commit d93e72dc7e
4 changed files with 47 additions and 62 deletions

View File

@ -1884,7 +1884,7 @@ public interface AvailableSettings {
/** /**
* JPA-standard variant of {@link #HBM2DDL_IMPORT_FILES} for specifying a database * JPA-standard variant of {@link #HBM2DDL_IMPORT_FILES} for specifying a database
* initialization script to be run as part of schema-export * initialization script to be run as part of schema-export
* * <p>
* Specifies a {@link java.io.Reader} configured for reading of the SQL load script * Specifies a {@link java.io.Reader} configured for reading of the SQL load script
* or a string designating the {@link java.net.URL} for the SQL load script. * or a string designating the {@link java.net.URL} for the SQL load script.
*/ */
@ -1894,7 +1894,7 @@ public interface AvailableSettings {
* The JPA variant of {@link #HBM2DDL_CREATE_NAMESPACES} used to specify whether database * The JPA variant of {@link #HBM2DDL_CREATE_NAMESPACES} used to specify whether database
* schemas used in the mapping model should be created on export in addition to creating * schemas used in the mapping model should be created on export in addition to creating
* the tables, sequences, etc. * the tables, sequences, etc.
* * <p>
* The default is {@code false}, meaning to not create schemas * The default is {@code false}, meaning to not create schemas
*/ */
String JAKARTA_HBM2DDL_CREATE_SCHEMAS = "jakarta.persistence.create-database-schemas"; String JAKARTA_HBM2DDL_CREATE_SCHEMAS = "jakarta.persistence.create-database-schemas";

View File

@ -389,8 +389,7 @@ public class MappingModelCreationHelper {
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext(); final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory(); final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext(); final Dialect dialect = sessionFactory.getSqlStringGenerationContext().getDialect();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final MappingMetamodel domainModel = creationContext.getDomainModel(); final MappingMetamodel domainModel = creationContext.getDomainModel();
final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor( bootValueMapping.getRole() ); final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor( bootValueMapping.getRole() );

View File

@ -25,7 +25,6 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.NaturalIdDataAccess; import org.hibernate.cache.spi.access.NaturalIdDataAccess;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
@ -488,21 +487,20 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
protected String generateSubquery(PersistentClass model, Metadata mapping) { protected String generateSubquery(PersistentClass model, Metadata mapping) {
Dialect dialect = getFactory().getJdbcServices().getDialect(); final Dialect dialect = getFactory().getJdbcServices().getDialect();
SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
if ( !model.hasSubclasses() ) { if ( !model.hasSubclasses() ) {
return model.getTable().getQualifiedName( sqlStringGenerationContext ); return model.getTable().getQualifiedName( getFactory().getSqlStringGenerationContext() );
} }
Set<Column> columns = new LinkedHashSet<>(); final Set<Column> columns = new LinkedHashSet<>();
for ( Table table : model.getSubclassTableClosure() ) { for ( Table table : model.getSubclassTableClosure() ) {
if ( !table.isAbstractUnionTable() ) { if ( !table.isAbstractUnionTable() ) {
columns.addAll( table.getColumns() ); columns.addAll( table.getColumns() );
} }
} }
StringBuilder buf = new StringBuilder() final StringBuilder subquery = new StringBuilder()
.append( "( " ); .append( "( " );
List<PersistentClass> classes = new JoinedList<>( List<PersistentClass> classes = new JoinedList<>(
@ -514,37 +512,29 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
Table table = clazz.getTable(); Table table = clazz.getTable();
if ( !table.isAbstractUnionTable() ) { if ( !table.isAbstractUnionTable() ) {
//TODO: move to .sql package!! //TODO: move to .sql package!!
buf.append( "select " ); if ( subquery.length() > 2 ) {
subquery.append( " union " );
if ( dialect.supportsUnionAll() ) {
subquery.append( "all " );
}
}
subquery.append( "select " );
for ( Column col : columns ) { for ( Column col : columns ) {
if ( !table.containsColumn(col) ) { if ( !table.containsColumn( col ) ) {
int sqlType = col.getSqlTypeCode( mapping ); int sqlType = col.getSqlTypeCode( mapping );
buf.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) ) subquery.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
.append(" as "); .append(" as ");
} }
buf.append(col.getQuotedName(dialect)); subquery.append( col.getQuotedName( dialect ) )
buf.append(", "); .append(", ");
}
buf.append( clazz.getSubclassId() )
.append( " as clazz_" );
buf.append( " from " )
.append(
table.getQualifiedName(
sqlStringGenerationContext
)
);
buf.append( " union " );
if ( dialect.supportsUnionAll() ) {
buf.append( "all " );
} }
subquery.append( clazz.getSubclassId() )
.append( " as clazz_ from " )
.append( table.getQualifiedName( getFactory().getSqlStringGenerationContext() ) );
} }
} }
if ( buf.length() > 2 ) { return subquery.append( " )" ).toString();
//chop the last union (all)
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
}
return buf.append( " )" ).toString();
} }
protected String generateSubquery(Set<String> treated) { protected String generateSubquery(Set<String> treated) {
@ -557,10 +547,9 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
// Collect all selectables of every entity subtype and group by selection expression as well as table name // Collect all selectables of every entity subtype and group by selection expression as well as table name
final LinkedHashMap<String, Map<String, SelectableMapping>> selectables = new LinkedHashMap<>(); final LinkedHashMap<String, Map<String, SelectableMapping>> selectables = new LinkedHashMap<>();
final SelectableConsumer selectableConsumer = (i, selectable) -> { final SelectableConsumer selectableConsumer = (i, selectable) ->
selectables.computeIfAbsent( selectable.getSelectionExpression(), k -> new HashMap<>() ) selectables.computeIfAbsent( selectable.getSelectionExpression(), k -> new HashMap<>() )
.put( selectable.getContainingTableExpression(), selectable ); .put( selectable.getContainingTableExpression(), selectable );
};
// Collect the concrete subclass table names for the treated entity names // Collect the concrete subclass table names for the treated entity names
final Set<String> treatedTableNames = new HashSet<>( treated.size() ); final Set<String> treatedTableNames = new HashSet<>( treated.size() );
for ( String subclassName : treated ) { for ( String subclassName : treated ) {
@ -589,6 +578,12 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor( name ); final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor( name );
final String subclassTableName = persister.getTableName(); final String subclassTableName = persister.getTableName();
if ( treatedTableNames.contains( subclassTableName ) ) { if ( treatedTableNames.contains( subclassTableName ) ) {
if ( buf.length() > 2 ) {
buf.append(" union ");
if ( dialect.supportsUnionAll() ) {
buf.append("all ");
}
}
buf.append( "select " ); buf.append( "select " );
for ( Map<String, SelectableMapping> selectableMappings : selectables.values() ) { for ( Map<String, SelectableMapping> selectableMappings : selectables.values() ) {
SelectableMapping selectableMapping = selectableMappings.get( subclassTableName ); SelectableMapping selectableMapping = selectableMappings.get( subclassTableName );
@ -603,20 +598,11 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
new ColumnReference( (String) null, selectableMapping ).appendReadExpression( sqlAppender ); new ColumnReference( (String) null, selectableMapping ).appendReadExpression( sqlAppender );
buf.append( ", " ); buf.append( ", " );
} }
buf.append( persister.getDiscriminatorSQLValue() ).append( " as clazz_" ); buf.append( persister.getDiscriminatorSQLValue() )
buf.append( " from " ).append( subclassTableName ); .append( " as clazz_ from " )
buf.append( " union " ); .append( subclassTableName );
if ( dialect.supportsUnionAll() ) {
buf.append( "all " );
} }
} }
}
if ( buf.length() > 2 ) {
//chop the last union (all)
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
}
return buf.append( " )" ).toString(); return buf.append( " )" ).toString();
} }

View File

@ -12,6 +12,7 @@ import java.util.regex.Pattern;
import org.hibernate.QueryException; import org.hibernate.QueryException;
import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.collection.SQLLoadableCollection; import org.hibernate.persister.collection.SQLLoadableCollection;
import org.hibernate.persister.entity.SQLLoadable; import org.hibernate.persister.entity.SQLLoadable;
@ -70,11 +71,14 @@ public class SQLQueryParser {
return sqlQuery; return sqlQuery;
} }
final SqlStringGenerationContext sqlStringGenerationContext = factory.getSqlStringGenerationContext();
final Identifier defaultCatalog = sqlStringGenerationContext.getDefaultCatalog();
final Identifier defaultSchema = sqlStringGenerationContext.getDefaultSchema();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final StringBuilder result = new StringBuilder( sqlQuery.length() + 20 ); final StringBuilder result = new StringBuilder( sqlQuery.length() + 20 );
int left, right; int left, right;
SqlStringGenerationContext sqlStringGenerationContext = factory.getSqlStringGenerationContext();
// replace {....} with corresponding column aliases // replace {....} with corresponding column aliases
for ( int curr = 0; curr < sqlQuery.length(); curr = right + 1 ) { for ( int curr = 0; curr < sqlQuery.length(); curr = right + 1 ) {
if ( ( left = sqlQuery.indexOf( '{', curr ) ) < 0 ) { if ( ( left = sqlQuery.indexOf( '{', curr ) ) < 0 ) {
@ -98,32 +102,28 @@ public class SQLQueryParser {
// Domain replacement // Domain replacement
switch ( aliasPath ) { switch ( aliasPath ) {
case DOMAIN_PLACEHOLDER: { case DOMAIN_PLACEHOLDER: {
final Identifier catalogName = sqlStringGenerationContext.getDefaultCatalog(); if ( defaultCatalog != null ) {
if ( catalogName != null ) { result.append( defaultCatalog.render(dialect) );
result.append( catalogName.render( sqlStringGenerationContext.getDialect() ) );
result.append( "." ); result.append( "." );
} }
final Identifier schemaName = sqlStringGenerationContext.getDefaultSchema(); if ( defaultSchema != null ) {
if ( schemaName != null ) { result.append( defaultSchema.render(dialect) );
result.append( schemaName.render( sqlStringGenerationContext.getDialect() ) );
result.append( "." ); result.append( "." );
} }
break; break;
} }
// Schema replacement // Schema replacement
case SCHEMA_PLACEHOLDER: { case SCHEMA_PLACEHOLDER: {
final Identifier schemaName = sqlStringGenerationContext.getDefaultSchema(); if ( defaultSchema != null ) {
if ( schemaName != null ) { result.append( defaultSchema.render(dialect) );
result.append( schemaName.render( sqlStringGenerationContext.getDialect() ) );
result.append( "." ); result.append( "." );
} }
break; break;
} }
// Catalog replacement // Catalog replacement
case CATALOG_PLACEHOLDER: { case CATALOG_PLACEHOLDER: {
final Identifier catalogName = sqlStringGenerationContext.getDefaultCatalog(); if ( defaultCatalog != null ) {
if ( catalogName != null ) { result.append( defaultCatalog.render(dialect) );
result.append( catalogName.render( sqlStringGenerationContext.getDialect() ) );
result.append( "." ); result.append( "." );
} }
break; break;