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

View File

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

View File

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