some minor cleanups

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-11-07 00:20:44 +01:00
parent de6aa69fb0
commit 7fda1e0ebd
3 changed files with 57 additions and 64 deletions

View File

@ -25,6 +25,7 @@ import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.SessionEventListener;
import org.hibernate.SessionException;
import org.hibernate.Transaction;
@ -76,6 +77,7 @@ import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaInsert;
import org.hibernate.query.criteria.JpaCriteriaInsertSelect;
import org.hibernate.query.hql.spi.SqmQueryImplementor;
import org.hibernate.query.named.NamedObjectRepository;
import org.hibernate.query.named.NamedResultSetMappingMemento;
import org.hibernate.query.spi.HqlInterpretation;
import org.hibernate.query.spi.QueryImplementor;
@ -85,7 +87,6 @@ import org.hibernate.query.sql.spi.NativeQueryImplementor;
import org.hibernate.query.sqm.SqmSelectionQuery;
import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.internal.SqmSelectionQueryImpl;
import org.hibernate.query.sqm.internal.SqmUtil;
import org.hibernate.query.sqm.spi.NamedSqmQueryMemento;
import org.hibernate.query.sqm.tree.SqmDmlStatement;
import org.hibernate.query.sqm.tree.SqmStatement;
@ -122,6 +123,7 @@ import static org.hibernate.internal.util.ReflectHelper.isClass;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.jpa.internal.util.FlushModeTypeHelper.getFlushModeType;
import static org.hibernate.query.sqm.internal.SqmUtil.verifyIsSelectStatement;
/**
* Base class for implementations of {@link org.hibernate.SharedSessionContract} and
@ -858,11 +860,11 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@Override
public <R> SelectionQuery<R> createSelectionQuery(CriteriaQuery<R> criteria) {
if ( criteria instanceof CriteriaDefinition ) {
return ((CriteriaDefinition<R>) criteria).createSelectionQuery(this);
if ( criteria instanceof CriteriaDefinition<R> criteriaDefinition ) {
return criteriaDefinition.createSelectionQuery(this);
}
else {
SqmUtil.verifyIsSelectStatement( (SqmStatement<?>) criteria, null );
verifyIsSelectStatement( (SqmStatement<?>) criteria, null );
return new SqmSelectionQueryImpl<>( (SqmSelectStatement<R>) criteria, criteria.getResultType(), this );
}
}
@ -940,8 +942,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
protected NamedResultSetMappingMemento getResultSetMappingMemento(String resultSetMappingName) {
final NamedResultSetMappingMemento resultSetMappingMemento =
getFactory().getQueryEngine().getNamedObjectRepository()
.getResultSetMappingMemento( resultSetMappingName );
namedObjectRepository().getResultSetMappingMemento( resultSetMappingName );
if ( resultSetMappingMemento == null ) {
throw new HibernateException( "Could not resolve specified result-set mapping name: "
+ resultSetMappingName );
@ -1048,14 +1049,16 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
);
}
private NamedObjectRepository namedObjectRepository() {
return getFactory().getQueryEngine().getNamedObjectRepository();
}
private NamedSqmQueryMemento getSqmQueryMemento(String queryName) {
return getFactory().getQueryEngine().getNamedObjectRepository()
.getSqmQueryMemento( queryName );
return namedObjectRepository().getSqmQueryMemento( queryName );
}
private NamedNativeQueryMemento getNativeQueryMemento(String queryName) {
return getFactory().getQueryEngine().getNamedObjectRepository()
.getNativeQueryMemento( queryName );
return namedObjectRepository().getNativeQueryMemento( queryName );
}
private <R> SelectionQuery<R> createNamedNativeSelectionQuery(
@ -1068,15 +1071,12 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
NamedSqmQueryMemento memento,
Class<R> expectedResultType) {
final SqmSelectionQuery<R> selectionQuery = memento.toSelectionQuery( expectedResultType, this );
if ( isEmpty( memento.getComment() ) ) {
selectionQuery.setComment( "Named query : " + memento.getRegistrationName() );
}
else {
selectionQuery.setComment( memento.getComment() );
}
final String comment = memento.getComment();
selectionQuery.setComment( isEmpty( comment ) ? "Named query : " + memento.getRegistrationName() : comment );
applyQuerySettingsAndHints( selectionQuery );
if ( memento.getLockOptions() != null ) {
selectionQuery.getLockOptions().overlay( memento.getLockOptions() );
final LockOptions lockOptions = memento.getLockOptions();
if ( lockOptions != null ) {
selectionQuery.getLockOptions().overlay( lockOptions );
}
return selectionQuery;
}
@ -1426,8 +1426,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@Override
public <T> QueryImplementor<T> createQuery(CriteriaQuery<T> criteriaQuery) {
checkOpen();
if ( criteriaQuery instanceof CriteriaDefinition ) {
return (QueryImplementor<T>) ((CriteriaDefinition<T>) criteriaQuery).createSelectionQuery(this);
if ( criteriaQuery instanceof CriteriaDefinition<T> criteriaDefinition ) {
return (QueryImplementor<T>) criteriaDefinition.createSelectionQuery(this);
}
else {
try {

View File

@ -1655,8 +1655,8 @@ public class SessionImpl
@Override
public <T> QueryImplementor<T> createQuery(CriteriaSelect<T> selectQuery) {
checkOpen();
if ( selectQuery instanceof CriteriaDefinition ) {
return (QueryImplementor<T>) ((CriteriaDefinition<T>) selectQuery).createSelectionQuery(this);
if ( selectQuery instanceof CriteriaDefinition<T> criteriaDefinition ) {
return (QueryImplementor<T>) criteriaDefinition.createSelectionQuery(this);
}
else {
try {

View File

@ -221,12 +221,11 @@ public class QuerySqmImpl<R>
// Parameters might be created through HibernateCriteriaBuilder.value which we need to bind here
for ( SqmParameter<?> sqmParameter : domainParameterXref.getParameterResolutions().getSqmParameters() ) {
if ( sqmParameter instanceof SqmJpaCriteriaParameterWrapper<?> ) {
bindCriteriaParameter((SqmJpaCriteriaParameterWrapper<?>) sqmParameter);
if ( sqmParameter instanceof SqmJpaCriteriaParameterWrapper<?> wrapper ) {
bindCriteriaParameter( wrapper );
}
}
if ( sqm instanceof SqmSelectStatement<?> ) {
final SqmSelectStatement<R> selectStatement = (SqmSelectStatement<R>) sqm;
if ( sqm instanceof SqmSelectStatement<R> selectStatement ) {
final SqmQueryPart<R> queryPart = selectStatement.getQueryPart();
// For criteria queries, we have to validate the fetch structure here
queryPart.validateQueryStructureAndFetchOwners();
@ -464,13 +463,13 @@ public class QuerySqmImpl<R>
private SelectQueryPlan<R> resolveSelectQueryPlan() {
final QueryInterpretationCache.Key cacheKey = createInterpretationsKey( this );
if ( cacheKey != null ) {
return getSession().getFactory().getQueryEngine().getInterpretationCache()
.resolveSelectQueryPlan( cacheKey, this::buildSelectQueryPlan );
}
else {
return buildSelectQueryPlan();
return cacheKey != null
? interpretationCache().resolveSelectQueryPlan( cacheKey, this::buildSelectQueryPlan )
: buildSelectQueryPlan();
}
private QueryInterpretationCache interpretationCache() {
return getSessionFactory().getQueryEngine().getInterpretationCache();
}
@ -525,8 +524,7 @@ public class QuerySqmImpl<R>
NonSelectQueryPlan queryPlan = null;
final QueryInterpretationCache.Key cacheKey = generateNonSelectKey( this );
final QueryInterpretationCache interpretationCache =
getSessionFactory().getQueryEngine().getInterpretationCache();
final QueryInterpretationCache interpretationCache = interpretationCache();
if ( cacheKey != null ) {
queryPlan = interpretationCache.getNonSelectQueryPlan( cacheKey );
}
@ -544,19 +542,19 @@ public class QuerySqmImpl<R>
private NonSelectQueryPlan buildNonSelectQueryPlan() {
// to get here the SQM statement has already been validated to be
// a non-select variety...
if ( getSqmStatement() instanceof SqmDeleteStatement<?> ) {
final SqmStatement<R> sqmStatement = getSqmStatement();
if ( sqmStatement instanceof SqmDeleteStatement<?> ) {
return buildDeleteQueryPlan();
}
if ( getSqmStatement() instanceof SqmUpdateStatement<?> ) {
else if ( sqmStatement instanceof SqmUpdateStatement<?> ) {
return buildUpdateQueryPlan();
}
if ( getSqmStatement() instanceof SqmInsertStatement<?> ) {
else if ( sqmStatement instanceof SqmInsertStatement<?> ) {
return buildInsertQueryPlan();
}
throw new UnsupportedOperationException( "Query#executeUpdate for Statements of type [" + getSqmStatement() + "] not supported" );
else {
throw new UnsupportedOperationException( "Query#executeUpdate for Statements of type [" + sqmStatement + "] not supported" );
}
}
private NonSelectQueryPlan buildDeleteQueryPlan() {
@ -569,8 +567,9 @@ public class QuerySqmImpl<R>
private NonSelectQueryPlan buildConcreteDeleteQueryPlan(SqmDeleteStatement<?> sqmDelete) {
final EntityDomainType<?> entityDomainType = sqmDelete.getTarget().getModel();
final String entityNameToDelete = entityDomainType.getHibernateEntityName();
final EntityPersister persister = getSessionFactory().getMappingMetamodel().getEntityDescriptor( entityNameToDelete );
final EntityPersister persister =
getSessionFactory().getMappingMetamodel()
.getEntityDescriptor( entityDomainType.getHibernateEntityName() );
final SqmMultiTableMutationStrategy multiTableStrategy = persister.getSqmMultiTableMutationStrategy();
if ( multiTableStrategy != null ) {
// NOTE : MultiTableDeleteQueryPlan and SqmMultiTableMutationStrategy already handle soft-deletes internally
@ -591,11 +590,9 @@ public class QuerySqmImpl<R>
private NonSelectQueryPlan buildUpdateQueryPlan() {
final SqmUpdateStatement<R> sqmUpdate = (SqmUpdateStatement<R>) getSqmStatement();
final String entityNameToUpdate = sqmUpdate.getTarget().getModel().getHibernateEntityName();
final EntityPersister persister =
getSessionFactory().getMappingMetamodel().getEntityDescriptor( entityNameToUpdate );
getSessionFactory().getMappingMetamodel()
.getEntityDescriptor( sqmUpdate.getTarget().getModel().getHibernateEntityName() );
final SqmMultiTableMutationStrategy multiTableStrategy = persister.getSqmMultiTableMutationStrategy();
return multiTableStrategy == null
? new SimpleUpdateQueryPlan( sqmUpdate, domainParameterXref )
@ -604,17 +601,16 @@ public class QuerySqmImpl<R>
private NonSelectQueryPlan buildInsertQueryPlan() {
final SqmInsertStatement<R> sqmInsert = (SqmInsertStatement<R>) getSqmStatement();
final String entityNameToInsert = sqmInsert.getTarget().getModel().getHibernateEntityName();
final EntityPersister persister =
getSessionFactory().getMappingMetamodel().getEntityDescriptor( entityNameToInsert );
getSessionFactory().getMappingMetamodel()
.getEntityDescriptor( sqmInsert.getTarget().getModel().getHibernateEntityName() );
boolean useMultiTableInsert = persister.hasMultipleTables();
if ( !useMultiTableInsert && !isSimpleValuesInsert( sqmInsert, persister ) ) {
final Generator identifierGenerator = persister.getGenerator();
if ( identifierGenerator instanceof BulkInsertionCapableIdentifierGenerator
&& identifierGenerator instanceof OptimizableGenerator ) {
final Optimizer optimizer = ( (OptimizableGenerator) identifierGenerator ).getOptimizer();
&& identifierGenerator instanceof OptimizableGenerator optimizableGenerator ) {
final Optimizer optimizer = optimizableGenerator.getOptimizer();
if ( optimizer != null && optimizer.getIncrementSize() > 1 ) {
useMultiTableInsert = !hasIdentifierAssigned( sqmInsert, persister );
}
@ -627,15 +623,15 @@ public class QuerySqmImpl<R>
persister.getSqmMultiTableInsertStrategy()
);
}
else if ( sqmInsert instanceof SqmInsertValuesStatement<?>
&& ( (SqmInsertValuesStatement<R>) sqmInsert ).getValuesList().size() != 1
else if ( sqmInsert instanceof SqmInsertValuesStatement<R> insertValues
&& insertValues.getValuesList().size() != 1
&& !getSessionFactory().getJdbcServices().getDialect().supportsValuesListForInsert() ) {
// Split insert-values queries if the dialect doesn't support values lists
final SqmInsertValuesStatement<R> insertValues = (SqmInsertValuesStatement<R>) sqmInsert;
final List<SqmValues> valuesList = insertValues.getValuesList();
final NonSelectQueryPlan[] planParts = new NonSelectQueryPlan[valuesList.size()];
for ( int i = 0; i < valuesList.size(); i++ ) {
final SqmInsertValuesStatement<?> subInsert = insertValues.copyWithoutValues( SqmCopyContext.simpleContext() );
final SqmInsertValuesStatement<?> subInsert =
insertValues.copyWithoutValues( SqmCopyContext.simpleContext() );
subInsert.values( valuesList.get( i ) );
planParts[i] = new SimpleInsertQueryPlan( subInsert, domainParameterXref );
}
@ -652,15 +648,12 @@ public class QuerySqmImpl<R>
? identifierMapping.getAttributeName()
: EntityIdentifierMapping.ID_ROLE_NAME;
for ( SqmPath<?> insertionTargetPath : sqmInsert.getInsertionTargetPaths() ) {
final SqmPath<?> lhs = insertionTargetPath.getLhs();
if ( !( lhs instanceof SqmRoot<?> ) ) {
continue;
}
final SqmPathSource<?> referencedPathSource = insertionTargetPath.getReferencedPathSource();
if ( referencedPathSource.getPathName().equals( partName ) ) {
if ( insertionTargetPath.getLhs() instanceof SqmRoot<?> ) {
if ( insertionTargetPath.getReferencedPathSource().getPathName().equals( partName ) ) {
return true;
}
}
}
return false;
}