minor code cleanups in AbstractEntityPersister

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-08-16 12:49:00 +02:00
parent 4a447ac819
commit afca93158e
2 changed files with 69 additions and 65 deletions

View File

@ -9,10 +9,13 @@ package org.hibernate.internal.util;
import java.io.Serializable; import java.io.Serializable;
/** /**
* @deprecated This is a legacy of very ancient versions of Hibernate.
*
* @author Gavin King * @author Gavin King
*/ */
@Deprecated
public class MarkerObject implements Serializable { public class MarkerObject implements Serializable {
private String name; private final String name;
public MarkerObject(String name) { public MarkerObject(String name) {
this.name = name; this.name = name;

View File

@ -117,9 +117,9 @@ import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.FilterHelper; import org.hibernate.internal.FilterHelper;
import org.hibernate.internal.util.IndexedConsumer; import org.hibernate.internal.util.IndexedConsumer;
import org.hibernate.internal.util.LazyValue; import org.hibernate.internal.util.LazyValue;
import org.hibernate.internal.util.MarkerObject;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.internal.util.collections.LockModeEnumMap; import org.hibernate.internal.util.collections.LockModeEnumMap;
import org.hibernate.jdbc.Expectation; import org.hibernate.jdbc.Expectation;
import org.hibernate.jdbc.TooManyRowsAffectedException; import org.hibernate.jdbc.TooManyRowsAffectedException;
@ -158,7 +158,6 @@ import org.hibernate.metamodel.mapping.Association;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList; import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.AttributeMappingsMap; import org.hibernate.metamodel.mapping.AttributeMappingsMap;
import org.hibernate.metamodel.mapping.AttributeMetadata;
import org.hibernate.metamodel.mapping.DiscriminatorConverter; import org.hibernate.metamodel.mapping.DiscriminatorConverter;
import org.hibernate.metamodel.mapping.DiscriminatorType; import org.hibernate.metamodel.mapping.DiscriminatorType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
@ -253,7 +252,6 @@ import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate; import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate;
import org.hibernate.sql.ast.tree.predicate.InListPredicate; import org.hibernate.sql.ast.tree.predicate.InListPredicate;
import org.hibernate.sql.ast.tree.predicate.Junction; import org.hibernate.sql.ast.tree.predicate.Junction;
import org.hibernate.sql.ast.tree.predicate.NegatedPredicate;
import org.hibernate.sql.ast.tree.predicate.NullnessPredicate; import org.hibernate.sql.ast.tree.predicate.NullnessPredicate;
import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
@ -313,6 +311,7 @@ import static org.hibernate.internal.util.collections.ArrayHelper.toTypeArray;
import static org.hibernate.internal.util.collections.CollectionHelper.combine; import static org.hibernate.internal.util.collections.CollectionHelper.combine;
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty; import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize; import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
import static org.hibernate.internal.util.collections.CollectionHelper.toSmallList;
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.supportsSqlArrayType; import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.supportsSqlArrayType;
import static org.hibernate.metamodel.RepresentationMode.POJO; import static org.hibernate.metamodel.RepresentationMode.POJO;
import static org.hibernate.persister.entity.DiscriminatorHelper.NOT_NULL_DISCRIMINATOR; import static org.hibernate.persister.entity.DiscriminatorHelper.NOT_NULL_DISCRIMINATOR;
@ -905,27 +904,25 @@ public abstract class AbstractEntityPersister
} }
} }
private boolean shouldUseShallowCacheLayout(CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) { private static CacheLayout queryCacheLayout(CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) {
final CacheLayout queryCacheLayout; return entityQueryCacheLayout == null ? options.getQueryCacheLayout() : entityQueryCacheLayout;
if ( entityQueryCacheLayout == null ) {
queryCacheLayout = options.getQueryCacheLayout();
}
else {
queryCacheLayout = entityQueryCacheLayout;
}
return queryCacheLayout == CacheLayout.SHALLOW || queryCacheLayout == CacheLayout.SHALLOW_WITH_DISCRIMINATOR
|| queryCacheLayout == CacheLayout.AUTO && ( canUseReferenceCacheEntries() || canReadFromCache() );
} }
private boolean shouldStoreDiscriminatorInShallowQueryCacheLayout(CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) { private boolean shouldUseShallowCacheLayout(CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) {
final CacheLayout queryCacheLayout; switch ( queryCacheLayout( entityQueryCacheLayout, options ) ) {
if ( entityQueryCacheLayout == null ) { case FULL:
queryCacheLayout = options.getQueryCacheLayout(); return false;
case AUTO:
return canUseReferenceCacheEntries()
|| canReadFromCache();
default:
return true;
} }
else { }
queryCacheLayout = entityQueryCacheLayout;
} private static boolean shouldStoreDiscriminatorInShallowQueryCacheLayout(
return queryCacheLayout == CacheLayout.SHALLOW_WITH_DISCRIMINATOR; CacheLayout entityQueryCacheLayout, SessionFactoryOptions options) {
return queryCacheLayout( entityQueryCacheLayout, options ) == CacheLayout.SHALLOW_WITH_DISCRIMINATOR;
} }
protected abstract String[] getSubclassTableNames(); protected abstract String[] getSubclassTableNames();
@ -1170,10 +1167,10 @@ public abstract class AbstractEntityPersister
@Override @Override
public Iterable<UniqueKeyEntry> uniqueKeyEntries() { public Iterable<UniqueKeyEntry> uniqueKeyEntries() {
if ( this.uniqueKeyEntries == null ) { if ( uniqueKeyEntries == null ) {
this.uniqueKeyEntries = initUniqueKeyEntries( this ); uniqueKeyEntries = initUniqueKeyEntries( this );
} }
return this.uniqueKeyEntries; return uniqueKeyEntries;
} }
private static List<UniqueKeyEntry> initUniqueKeyEntries(final AbstractEntityPersister aep) { private static List<UniqueKeyEntry> initUniqueKeyEntries(final AbstractEntityPersister aep) {
@ -1192,7 +1189,7 @@ public abstract class AbstractEntityPersister
} }
} }
} }
return CollectionHelper.toSmallList( uniqueKeys ); return toSmallList( uniqueKeys );
} }
protected Map<String, SingleIdArrayLoadPlan> getLazyLoadPlanByFetchGroup() { protected Map<String, SingleIdArrayLoadPlan> getLazyLoadPlanByFetchGroup() {
@ -1230,7 +1227,7 @@ public abstract class AbstractEntityPersister
return null; return null;
} }
else { else {
JdbcParametersList.Builder jdbcParametersBuilder = JdbcParametersList.newBuilder(); final JdbcParametersList.Builder jdbcParametersBuilder = JdbcParametersList.newBuilder();
final SelectStatement select = LoaderSelectBuilder.createSelect( final SelectStatement select = LoaderSelectBuilder.createSelect(
this, this,
partsToSelect, partsToSelect,
@ -1242,12 +1239,11 @@ public abstract class AbstractEntityPersister
jdbcParametersBuilder::add, jdbcParametersBuilder::add,
factory factory
); );
JdbcParametersList jdbcParameters = jdbcParametersBuilder.build();
return new SingleIdArrayLoadPlan( return new SingleIdArrayLoadPlan(
this, this,
getIdentifierMapping(), getIdentifierMapping(),
select, select,
jdbcParameters, jdbcParametersBuilder.build(),
LockOptions.NONE, LockOptions.NONE,
factory factory
); );
@ -2921,17 +2917,16 @@ public abstract class AbstractEntityPersister
return new NullnessPredicate( sqlExpression ); return new NullnessPredicate( sqlExpression );
} }
else if ( value == NOT_NULL_DISCRIMINATOR ) { else if ( value == NOT_NULL_DISCRIMINATOR ) {
return new NegatedPredicate( new NullnessPredicate( sqlExpression ) ); return new NullnessPredicate( sqlExpression, true );
} }
else { else {
final QueryLiteral<Object> literal = new QueryLiteral<>( value, discriminatorType ); return new ComparisonPredicate( sqlExpression, ComparisonOperator.EQUAL,
return new ComparisonPredicate( sqlExpression, ComparisonOperator.EQUAL, literal ); new QueryLiteral<>( value, discriminatorType ) );
} }
} }
} }
private Predicate createInListPredicate(BasicType<?> discriminatorType, Expression sqlExpression) { private Predicate createInListPredicate(BasicType<?> discriminatorType, Expression sqlExpression) {
final List<Expression> values = new ArrayList<>( fullDiscriminatorValues.length );
boolean hasNull = false, hasNonNull = false; boolean hasNull = false, hasNonNull = false;
for ( Object discriminatorValue : fullDiscriminatorValues ) { for ( Object discriminatorValue : fullDiscriminatorValues ) {
if ( discriminatorValue == NULL_DISCRIMINATOR ) { if ( discriminatorValue == NULL_DISCRIMINATOR ) {
@ -2940,43 +2935,48 @@ public abstract class AbstractEntityPersister
else if ( discriminatorValue == NOT_NULL_DISCRIMINATOR ) { else if ( discriminatorValue == NOT_NULL_DISCRIMINATOR ) {
hasNonNull = true; hasNonNull = true;
} }
else { }
if ( hasNull && hasNonNull ) {
// This means we need to select all rows,
// and so we don't need a predicate at all
// Just return an empty Junction
return new Junction( Junction.Nature.DISJUNCTION );
}
else if ( hasNonNull ) {
// we need every row with a non-null discriminator
return new NullnessPredicate( sqlExpression, true );
}
else if ( hasNull ) {
final Junction junction = new Junction( Junction.Nature.DISJUNCTION );
junction.add( new NullnessPredicate( sqlExpression ) );
junction.add( discriminatorValuesPredicate( discriminatorType, sqlExpression ) );
return junction;
}
else {
return discriminatorValuesPredicate( discriminatorType, sqlExpression );
}
}
private InListPredicate discriminatorValuesPredicate(BasicType<?> discriminatorType, Expression sqlExpression) {
final List<Expression> values = new ArrayList<>( fullDiscriminatorValues.length );
for ( Object discriminatorValue : fullDiscriminatorValues ) {
if ( !(discriminatorValue instanceof MarkerObject) ) {
values.add( new QueryLiteral<>( discriminatorValue, discriminatorType) ); values.add( new QueryLiteral<>( discriminatorValue, discriminatorType) );
} }
} }
final Predicate predicate = new InListPredicate( sqlExpression, values ); return new InListPredicate( sqlExpression, values);
if ( hasNull || hasNonNull ) {
final Junction junction = new Junction( Junction.Nature.DISJUNCTION );
if ( hasNull && hasNonNull ) {
// This means we need to select everything, we don't need a predicate at all
// Return an empty Junction
return junction;
}
if ( hasNonNull ) {
return new NullnessPredicate( sqlExpression, true );
}
else if ( hasNull ) {
junction.add( new NullnessPredicate( sqlExpression ) );
}
junction.add( predicate );
return junction;
}
return predicate;
} }
protected String getPrunedDiscriminatorPredicate( protected String getPrunedDiscriminatorPredicate(
Map<String, EntityNameUse> entityNameUses, Map<String, EntityNameUse> entityNameUses,
MappingMetamodelImplementor mappingMetamodel, MappingMetamodelImplementor mappingMetamodel,
String alias) { String alias) {
final InFragment frag = new InFragment(); final InFragment fragment = new InFragment();
if ( isDiscriminatorFormula() ) { if ( isDiscriminatorFormula() ) {
frag.setFormula( alias, getDiscriminatorFormulaTemplate() ); fragment.setFormula( alias, getDiscriminatorFormulaTemplate() );
} }
else { else {
frag.setColumn( alias, getDiscriminatorColumnName() ); fragment.setColumn( alias, getDiscriminatorColumnName() );
} }
boolean containsNotNull = false; boolean containsNotNull = false;
for ( Map.Entry<String, EntityNameUse> entry : entityNameUses.entrySet() ) { for ( Map.Entry<String, EntityNameUse> entry : entityNameUses.entrySet() ) {
@ -2991,11 +2991,12 @@ public abstract class AbstractEntityPersister
// as the query will contain a filter for that already anyway // as the query will contain a filter for that already anyway
if ( !persister.isAbstract() && ( this == persister || !isTypeOrSuperType( persister ) ) ) { if ( !persister.isAbstract() && ( this == persister || !isTypeOrSuperType( persister ) ) ) {
containsNotNull = containsNotNull || InFragment.NOT_NULL.equals( persister.getDiscriminatorSQLValue() ); containsNotNull = containsNotNull || InFragment.NOT_NULL.equals( persister.getDiscriminatorSQLValue() );
frag.addValue( persister.getDiscriminatorSQLValue() ); fragment.addValue( persister.getDiscriminatorSQLValue() );
} }
} }
final List<String> discriminatorSQLValues = Arrays.asList( ( (AbstractEntityPersister) getRootEntityDescriptor() ).fullDiscriminatorSQLValues ); final AbstractEntityPersister rootEntityDescriptor = (AbstractEntityPersister) getRootEntityDescriptor();
if ( frag.getValues().size() == discriminatorSQLValues.size() ) { final List<String> discriminatorSQLValues = Arrays.asList( rootEntityDescriptor.fullDiscriminatorSQLValues );
if ( fragment.getValues().size() == discriminatorSQLValues.size() ) {
// Nothing to prune if we filter for all subtypes // Nothing to prune if we filter for all subtypes
return null; return null;
} }
@ -3010,7 +3011,7 @@ public abstract class AbstractEntityPersister
} }
final List<String> actualDiscriminatorSQLValues = new ArrayList<>( discriminatorSQLValues.size() ); final List<String> actualDiscriminatorSQLValues = new ArrayList<>( discriminatorSQLValues.size() );
for ( String value : discriminatorSQLValues ) { for ( String value : discriminatorSQLValues ) {
if ( !frag.getValues().contains( value ) && !InFragment.NULL.equals( value ) ) { if ( !fragment.getValues().contains( value ) && !InFragment.NULL.equals( value ) ) {
actualDiscriminatorSQLValues.add( value ); actualDiscriminatorSQLValues.add( value );
} }
} }
@ -3021,11 +3022,11 @@ public abstract class AbstractEntityPersister
sb.append( ") and " ); sb.append( ") and " );
} }
sb.append( lhs ).append( " is not null" ); sb.append( lhs ).append( " is not null" );
frag.getValues().remove( InFragment.NOT_NULL ); fragment.getValues().remove( InFragment.NOT_NULL );
return frag.toFragmentString() + sb; return fragment.toFragmentString() + sb;
} }
else { else {
return frag.toFragmentString(); return fragment.toFragmentString();
} }
} }