Skip to content

Commit d93e72d

Browse files
committed
some misc minor cleanups
1 parent 782d2c9 commit d93e72d

File tree

4 files changed

+47
-62
lines changed

4 files changed

+47
-62
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ public interface AvailableSettings {
18841884
/**
18851885
* JPA-standard variant of {@link #HBM2DDL_IMPORT_FILES} for specifying a database
18861886
* initialization script to be run as part of schema-export
1887-
*
1887+
* <p>
18881888
* Specifies a {@link java.io.Reader} configured for reading of the SQL load script
18891889
* or a string designating the {@link java.net.URL} for the SQL load script.
18901890
*/
@@ -1894,7 +1894,7 @@ public interface AvailableSettings {
18941894
* The JPA variant of {@link #HBM2DDL_CREATE_NAMESPACES} used to specify whether database
18951895
* schemas used in the mapping model should be created on export in addition to creating
18961896
* the tables, sequences, etc.
1897-
*
1897+
* <p>
18981898
* The default is {@code false}, meaning to not create schemas
18991899
*/
19001900
String JAKARTA_HBM2DDL_CREATE_SCHEMAS = "jakarta.persistence.create-database-schemas";

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ public static PluralAttributeMapping buildPluralAttributeMapping(
389389

390390
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
391391
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
392-
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
393-
final Dialect dialect = sqlStringGenerationContext.getDialect();
392+
final Dialect dialect = sessionFactory.getSqlStringGenerationContext().getDialect();
394393
final MappingMetamodel domainModel = creationContext.getDomainModel();
395394

396395
final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor( bootValueMapping.getRole() );

hibernate-core/src/main/java/org/hibernate/persister/entity/UnionSubclassEntityPersister.java

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.hibernate.HibernateException;
2626
import org.hibernate.MappingException;
2727
import org.hibernate.boot.Metadata;
28-
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
2928
import org.hibernate.cache.spi.access.EntityDataAccess;
3029
import org.hibernate.cache.spi.access.NaturalIdDataAccess;
3130
import org.hibernate.dialect.Dialect;
@@ -488,21 +487,20 @@ protected int[] getPropertyTableNumbers() {
488487

489488
protected String generateSubquery(PersistentClass model, Metadata mapping) {
490489

491-
Dialect dialect = getFactory().getJdbcServices().getDialect();
492-
SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
490+
final Dialect dialect = getFactory().getJdbcServices().getDialect();
493491

494492
if ( !model.hasSubclasses() ) {
495-
return model.getTable().getQualifiedName( sqlStringGenerationContext );
493+
return model.getTable().getQualifiedName( getFactory().getSqlStringGenerationContext() );
496494
}
497495

498-
Set<Column> columns = new LinkedHashSet<>();
496+
final Set<Column> columns = new LinkedHashSet<>();
499497
for ( Table table : model.getSubclassTableClosure() ) {
500498
if ( !table.isAbstractUnionTable() ) {
501499
columns.addAll( table.getColumns() );
502500
}
503501
}
504502

505-
StringBuilder buf = new StringBuilder()
503+
final StringBuilder subquery = new StringBuilder()
506504
.append( "( " );
507505

508506
List<PersistentClass> classes = new JoinedList<>(
@@ -514,37 +512,29 @@ protected String generateSubquery(PersistentClass model, Metadata mapping) {
514512
Table table = clazz.getTable();
515513
if ( !table.isAbstractUnionTable() ) {
516514
//TODO: move to .sql package!!
517-
buf.append( "select " );
515+
if ( subquery.length() > 2 ) {
516+
subquery.append( " union " );
517+
if ( dialect.supportsUnionAll() ) {
518+
subquery.append( "all " );
519+
}
520+
}
521+
subquery.append( "select " );
518522
for ( Column col : columns ) {
519-
if ( !table.containsColumn(col) ) {
523+
if ( !table.containsColumn( col ) ) {
520524
int sqlType = col.getSqlTypeCode( mapping );
521-
buf.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
525+
subquery.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
522526
.append(" as ");
523527
}
524-
buf.append(col.getQuotedName(dialect));
525-
buf.append(", ");
526-
}
527-
buf.append( clazz.getSubclassId() )
528-
.append( " as clazz_" );
529-
buf.append( " from " )
530-
.append(
531-
table.getQualifiedName(
532-
sqlStringGenerationContext
533-
)
534-
);
535-
buf.append( " union " );
536-
if ( dialect.supportsUnionAll() ) {
537-
buf.append( "all " );
528+
subquery.append( col.getQuotedName( dialect ) )
529+
.append(", ");
538530
}
531+
subquery.append( clazz.getSubclassId() )
532+
.append( " as clazz_ from " )
533+
.append( table.getQualifiedName( getFactory().getSqlStringGenerationContext() ) );
539534
}
540535
}
541536

542-
if ( buf.length() > 2 ) {
543-
//chop the last union (all)
544-
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
545-
}
546-
547-
return buf.append( " )" ).toString();
537+
return subquery.append( " )" ).toString();
548538
}
549539

550540
protected String generateSubquery(Set<String> treated) {
@@ -557,10 +547,9 @@ protected String generateSubquery(Set<String> treated) {
557547

558548
// Collect all selectables of every entity subtype and group by selection expression as well as table name
559549
final LinkedHashMap<String, Map<String, SelectableMapping>> selectables = new LinkedHashMap<>();
560-
final SelectableConsumer selectableConsumer = (i, selectable) -> {
550+
final SelectableConsumer selectableConsumer = (i, selectable) ->
561551
selectables.computeIfAbsent( selectable.getSelectionExpression(), k -> new HashMap<>() )
562552
.put( selectable.getContainingTableExpression(), selectable );
563-
};
564553
// Collect the concrete subclass table names for the treated entity names
565554
final Set<String> treatedTableNames = new HashSet<>( treated.size() );
566555
for ( String subclassName : treated ) {
@@ -589,6 +578,12 @@ protected String generateSubquery(Set<String> treated) {
589578
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor( name );
590579
final String subclassTableName = persister.getTableName();
591580
if ( treatedTableNames.contains( subclassTableName ) ) {
581+
if ( buf.length() > 2 ) {
582+
buf.append(" union ");
583+
if ( dialect.supportsUnionAll() ) {
584+
buf.append("all ");
585+
}
586+
}
592587
buf.append( "select " );
593588
for ( Map<String, SelectableMapping> selectableMappings : selectables.values() ) {
594589
SelectableMapping selectableMapping = selectableMappings.get( subclassTableName );
@@ -603,20 +598,11 @@ protected String generateSubquery(Set<String> treated) {
603598
new ColumnReference( (String) null, selectableMapping ).appendReadExpression( sqlAppender );
604599
buf.append( ", " );
605600
}
606-
buf.append( persister.getDiscriminatorSQLValue() ).append( " as clazz_" );
607-
buf.append( " from " ).append( subclassTableName );
608-
buf.append( " union " );
609-
if ( dialect.supportsUnionAll() ) {
610-
buf.append( "all " );
611-
}
601+
buf.append( persister.getDiscriminatorSQLValue() )
602+
.append( " as clazz_ from " )
603+
.append( subclassTableName );
612604
}
613605
}
614-
615-
if ( buf.length() > 2 ) {
616-
//chop the last union (all)
617-
buf.setLength( buf.length() - ( dialect.supportsUnionAll() ? 11 : 7 ) );
618-
}
619-
620606
return buf.append( " )" ).toString();
621607
}
622608

hibernate-core/src/main/java/org/hibernate/query/sql/internal/SQLQueryParser.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.hibernate.QueryException;
1313
import org.hibernate.boot.model.naming.Identifier;
1414
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
15+
import org.hibernate.dialect.Dialect;
1516
import org.hibernate.engine.spi.SessionFactoryImplementor;
1617
import org.hibernate.persister.collection.SQLLoadableCollection;
1718
import org.hibernate.persister.entity.SQLLoadable;
@@ -70,11 +71,14 @@ protected String substituteBrackets(String sqlQuery) throws QueryException {
7071
return sqlQuery;
7172
}
7273

73-
final StringBuilder result = new StringBuilder( sqlQuery.length() + 20 );
74-
int left, right;
74+
final SqlStringGenerationContext sqlStringGenerationContext = factory.getSqlStringGenerationContext();
75+
final Identifier defaultCatalog = sqlStringGenerationContext.getDefaultCatalog();
76+
final Identifier defaultSchema = sqlStringGenerationContext.getDefaultSchema();
77+
final Dialect dialect = sqlStringGenerationContext.getDialect();
7578

76-
SqlStringGenerationContext sqlStringGenerationContext = factory.getSqlStringGenerationContext();
79+
final StringBuilder result = new StringBuilder( sqlQuery.length() + 20 );
7780

81+
int left, right;
7882
// replace {....} with corresponding column aliases
7983
for ( int curr = 0; curr < sqlQuery.length(); curr = right + 1 ) {
8084
if ( ( left = sqlQuery.indexOf( '{', curr ) ) < 0 ) {
@@ -98,32 +102,28 @@ protected String substituteBrackets(String sqlQuery) throws QueryException {
98102
// Domain replacement
99103
switch ( aliasPath ) {
100104
case DOMAIN_PLACEHOLDER: {
101-
final Identifier catalogName = sqlStringGenerationContext.getDefaultCatalog();
102-
if ( catalogName != null ) {
103-
result.append( catalogName.render( sqlStringGenerationContext.getDialect() ) );
105+
if ( defaultCatalog != null ) {
106+
result.append( defaultCatalog.render(dialect) );
104107
result.append( "." );
105108
}
106-
final Identifier schemaName = sqlStringGenerationContext.getDefaultSchema();
107-
if ( schemaName != null ) {
108-
result.append( schemaName.render( sqlStringGenerationContext.getDialect() ) );
109+
if ( defaultSchema != null ) {
110+
result.append( defaultSchema.render(dialect) );
109111
result.append( "." );
110112
}
111113
break;
112114
}
113115
// Schema replacement
114116
case SCHEMA_PLACEHOLDER: {
115-
final Identifier schemaName = sqlStringGenerationContext.getDefaultSchema();
116-
if ( schemaName != null ) {
117-
result.append( schemaName.render( sqlStringGenerationContext.getDialect() ) );
117+
if ( defaultSchema != null ) {
118+
result.append( defaultSchema.render(dialect) );
118119
result.append( "." );
119120
}
120121
break;
121122
}
122123
// Catalog replacement
123124
case CATALOG_PLACEHOLDER: {
124-
final Identifier catalogName = sqlStringGenerationContext.getDefaultCatalog();
125-
if ( catalogName != null ) {
126-
result.append( catalogName.render( sqlStringGenerationContext.getDialect() ) );
125+
if ( defaultCatalog != null ) {
126+
result.append( defaultCatalog.render(dialect) );
127127
result.append( "." );
128128
}
129129
break;

0 commit comments

Comments
 (0)