HHH-16798 Fix check for supertype-treats in single table inheritance

Also, fix a small bug in special not-null discriminator predicate rendering
This commit is contained in:
Marco Belladelli 2023-08-01 10:01:20 +02:00
parent dbaf309050
commit 317334f14d
2 changed files with 15 additions and 8 deletions

View File

@ -28,6 +28,7 @@ import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
@ -3208,21 +3209,26 @@ public abstract class AbstractEntityPersister
}
if ( containsNotNull ) {
final StringBuilder sb = new StringBuilder();
String lhs;
final String lhs;
if ( isDiscriminatorFormula() ) {
lhs = StringHelper.replace( getDiscriminatorFormulaTemplate(), Template.TEMPLATE, alias );
}
else {
lhs = qualifyConditionally( alias, getDiscriminatorColumnName() );
}
sb.append( " or " ).append( lhs ).append( " is not in (" );
for ( Object discriminatorSQLValue : discriminatorSQLValues ) {
if ( !frag.getValues().contains( discriminatorSQLValue ) ) {
sb.append( lhs ).append( discriminatorSQLValue );
final List<String> actualDiscriminatorSQLValues = new ArrayList<>( discriminatorSQLValues.size() );
for ( String value : discriminatorSQLValues ) {
if ( !frag.getValues().contains( value ) && !InFragment.NULL.equals( value ) ) {
actualDiscriminatorSQLValues.add( value );
}
}
sb.append( ") and " ).append( lhs ).append( " is not null" );
final StringBuilder sb = new StringBuilder( 70 + actualDiscriminatorSQLValues.size() * 10 ).append( " or " );
if ( !actualDiscriminatorSQLValues.isEmpty() ) {
sb.append( lhs ).append( " is not in (" );
sb.append( String.join( ",", actualDiscriminatorSQLValues ) );
sb.append( ") and " );
}
sb.append( lhs ).append( " is not null" );
frag.getValues().remove( InFragment.NOT_NULL );
return frag.toFragmentString() + sb;
}

View File

@ -677,7 +677,8 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
// Filtering for abstract entities makes no sense, so ignore that
// Also, it makes no sense to filter for any of the super types,
// as the query will contain a filter for that already anyway
if ( !persister.isAbstract() && !isTypeOrSuperType( persister ) && useKind == EntityNameUse.UseKind.TREAT ) {
if ( useKind == EntityNameUse.UseKind.TREAT && !persister.isAbstract()
&& ( getSuperMappingType() == null || !getSuperMappingType().isTypeOrSuperType( persister ) ) ) {
containsTreatUse = true;
break;
}