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
* 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
* 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
* schemas used in the mapping model should be created on export in addition to creating
* the tables, sequences, etc.
*
* <p>
* The default is {@code false}, meaning to not create 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 SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final Dialect dialect = sessionFactory.getSqlStringGenerationContext().getDialect();
final MappingMetamodel domainModel = creationContext.getDomainModel();
final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor( bootValueMapping.getRole() );

View File

@ -25,7 +25,6 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
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.NaturalIdDataAccess;
import org.hibernate.dialect.Dialect;
@ -488,21 +487,20 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
protected String generateSubquery(PersistentClass model, Metadata mapping) {
Dialect dialect = getFactory().getJdbcServices().getDialect();
SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
final Dialect dialect = getFactory().getJdbcServices().getDialect();
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() ) {
if ( !table.isAbstractUnionTable() ) {
columns.addAll( table.getColumns() );
}
}
StringBuilder buf = new StringBuilder()
final StringBuilder subquery = new StringBuilder()
.append( "( " );
List<PersistentClass> classes = new JoinedList<>(
@ -514,37 +512,29 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
Table table = clazz.getTable();
if ( !table.isAbstractUnionTable() ) {
//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 ) {
if ( !table.containsColumn(col) ) {
if ( !table.containsColumn( col ) ) {
int sqlType = col.getSqlTypeCode( mapping );
buf.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
subquery.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
.append(" as ");
}
buf.append(col.getQuotedName(dialect));
buf.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( col.getQuotedName( dialect ) )
.append(", ");
}
subquery.append( clazz.getSubclassId() )
.append( " as clazz_ from " )
.append( table.getQualifiedName( getFactory().getSqlStringGenerationContext() ) );
}
}
if ( buf.length() > 2 ) {
//chop the last union (all)
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
}
return buf.append( " )" ).toString();
return subquery.append( " )" ).toString();
}
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
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<>() )
.put( selectable.getContainingTableExpression(), selectable );
};
// Collect the concrete subclass table names for the treated entity names
final Set<String> treatedTableNames = new HashSet<>( treated.size() );
for ( String subclassName : treated ) {
@ -589,6 +578,12 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor( name );
final String subclassTableName = persister.getTableName();
if ( treatedTableNames.contains( subclassTableName ) ) {
if ( buf.length() > 2 ) {
buf.append(" union ");
if ( dialect.supportsUnionAll() ) {
buf.append("all ");
}
}
buf.append( "select " );
for ( Map<String, SelectableMapping> selectableMappings : selectables.values() ) {
SelectableMapping selectableMapping = selectableMappings.get( subclassTableName );
@ -603,20 +598,11 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
new ColumnReference( (String) null, selectableMapping ).appendReadExpression( sqlAppender );
buf.append( ", " );
}
buf.append( persister.getDiscriminatorSQLValue() ).append( " as clazz_" );
buf.append( " from " ).append( subclassTableName );
buf.append( " union " );
if ( dialect.supportsUnionAll() ) {
buf.append( "all " );
}
buf.append( persister.getDiscriminatorSQLValue() )
.append( " as clazz_ from " )
.append( subclassTableName );
}
}
if ( buf.length() > 2 ) {
//chop the last union (all)
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
}
return buf.append( " )" ).toString();
}

View File

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