25
25
import org .hibernate .HibernateException ;
26
26
import org .hibernate .MappingException ;
27
27
import org .hibernate .boot .Metadata ;
28
- import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
29
28
import org .hibernate .cache .spi .access .EntityDataAccess ;
30
29
import org .hibernate .cache .spi .access .NaturalIdDataAccess ;
31
30
import org .hibernate .dialect .Dialect ;
@@ -488,21 +487,20 @@ protected int[] getPropertyTableNumbers() {
488
487
489
488
protected String generateSubquery (PersistentClass model , Metadata mapping ) {
490
489
491
- Dialect dialect = getFactory ().getJdbcServices ().getDialect ();
492
- SqlStringGenerationContext sqlStringGenerationContext = getFactory ().getSqlStringGenerationContext ();
490
+ final Dialect dialect = getFactory ().getJdbcServices ().getDialect ();
493
491
494
492
if ( !model .hasSubclasses () ) {
495
- return model .getTable ().getQualifiedName ( sqlStringGenerationContext );
493
+ return model .getTable ().getQualifiedName ( getFactory (). getSqlStringGenerationContext () );
496
494
}
497
495
498
- Set <Column > columns = new LinkedHashSet <>();
496
+ final Set <Column > columns = new LinkedHashSet <>();
499
497
for ( Table table : model .getSubclassTableClosure () ) {
500
498
if ( !table .isAbstractUnionTable () ) {
501
499
columns .addAll ( table .getColumns () );
502
500
}
503
501
}
504
502
505
- StringBuilder buf = new StringBuilder ()
503
+ final StringBuilder subquery = new StringBuilder ()
506
504
.append ( "( " );
507
505
508
506
List <PersistentClass > classes = new JoinedList <>(
@@ -514,37 +512,29 @@ protected String generateSubquery(PersistentClass model, Metadata mapping) {
514
512
Table table = clazz .getTable ();
515
513
if ( !table .isAbstractUnionTable () ) {
516
514
//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 " );
518
522
for ( Column col : columns ) {
519
- if ( !table .containsColumn (col ) ) {
523
+ if ( !table .containsColumn ( col ) ) {
520
524
int sqlType = col .getSqlTypeCode ( mapping );
521
- buf .append ( dialect .getSelectClauseNullString ( sqlType , getFactory ().getTypeConfiguration () ) )
525
+ subquery .append ( dialect .getSelectClauseNullString ( sqlType , getFactory ().getTypeConfiguration () ) )
522
526
.append (" as " );
523
527
}
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 (", " );
538
530
}
531
+ subquery .append ( clazz .getSubclassId () )
532
+ .append ( " as clazz_ from " )
533
+ .append ( table .getQualifiedName ( getFactory ().getSqlStringGenerationContext () ) );
539
534
}
540
535
}
541
536
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 ();
548
538
}
549
539
550
540
protected String generateSubquery (Set <String > treated ) {
@@ -557,10 +547,9 @@ protected String generateSubquery(Set<String> treated) {
557
547
558
548
// Collect all selectables of every entity subtype and group by selection expression as well as table name
559
549
final LinkedHashMap <String , Map <String , SelectableMapping >> selectables = new LinkedHashMap <>();
560
- final SelectableConsumer selectableConsumer = (i , selectable ) -> {
550
+ final SelectableConsumer selectableConsumer = (i , selectable ) ->
561
551
selectables .computeIfAbsent ( selectable .getSelectionExpression (), k -> new HashMap <>() )
562
552
.put ( selectable .getContainingTableExpression (), selectable );
563
- };
564
553
// Collect the concrete subclass table names for the treated entity names
565
554
final Set <String > treatedTableNames = new HashSet <>( treated .size () );
566
555
for ( String subclassName : treated ) {
@@ -589,6 +578,12 @@ protected String generateSubquery(Set<String> treated) {
589
578
final AbstractEntityPersister persister = (AbstractEntityPersister ) metamodel .findEntityDescriptor ( name );
590
579
final String subclassTableName = persister .getTableName ();
591
580
if ( treatedTableNames .contains ( subclassTableName ) ) {
581
+ if ( buf .length () > 2 ) {
582
+ buf .append (" union " );
583
+ if ( dialect .supportsUnionAll () ) {
584
+ buf .append ("all " );
585
+ }
586
+ }
592
587
buf .append ( "select " );
593
588
for ( Map <String , SelectableMapping > selectableMappings : selectables .values () ) {
594
589
SelectableMapping selectableMapping = selectableMappings .get ( subclassTableName );
@@ -603,20 +598,11 @@ protected String generateSubquery(Set<String> treated) {
603
598
new ColumnReference ( (String ) null , selectableMapping ).appendReadExpression ( sqlAppender );
604
599
buf .append ( ", " );
605
600
}
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 );
612
604
}
613
605
}
614
-
615
- if ( buf .length () > 2 ) {
616
- //chop the last union (all)
617
- buf .setLength ( buf .length () - ( dialect .supportsUnionAll () ? 11 : 7 ) );
618
- }
619
-
620
606
return buf .append ( " )" ).toString ();
621
607
}
622
608
0 commit comments