diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index db36fa91c2..9a98a01aa6 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -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 SelectionQuery createSelectionQuery(CriteriaQuery criteria) { - if ( criteria instanceof CriteriaDefinition ) { - return ((CriteriaDefinition) criteria).createSelectionQuery(this); + if ( criteria instanceof CriteriaDefinition criteriaDefinition ) { + return criteriaDefinition.createSelectionQuery(this); } else { - SqmUtil.verifyIsSelectStatement( (SqmStatement) criteria, null ); + verifyIsSelectStatement( (SqmStatement) criteria, null ); return new SqmSelectionQueryImpl<>( (SqmSelectStatement) 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 SelectionQuery createNamedNativeSelectionQuery( @@ -1068,15 +1071,12 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont NamedSqmQueryMemento memento, Class expectedResultType) { final SqmSelectionQuery 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 QueryImplementor createQuery(CriteriaQuery criteriaQuery) { checkOpen(); - if ( criteriaQuery instanceof CriteriaDefinition ) { - return (QueryImplementor) ((CriteriaDefinition) criteriaQuery).createSelectionQuery(this); + if ( criteriaQuery instanceof CriteriaDefinition criteriaDefinition ) { + return (QueryImplementor) criteriaDefinition.createSelectionQuery(this); } else { try { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 8ae84702a2..e524285d34 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1655,8 +1655,8 @@ public class SessionImpl @Override public QueryImplementor createQuery(CriteriaSelect selectQuery) { checkOpen(); - if ( selectQuery instanceof CriteriaDefinition ) { - return (QueryImplementor) ((CriteriaDefinition) selectQuery).createSelectionQuery(this); + if ( selectQuery instanceof CriteriaDefinition criteriaDefinition ) { + return (QueryImplementor) criteriaDefinition.createSelectionQuery(this); } else { try { diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java index 288cbc0171..de8430f4a1 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java @@ -221,12 +221,11 @@ public class QuerySqmImpl // 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 selectStatement = (SqmSelectStatement) sqm; + if ( sqm instanceof SqmSelectStatement selectStatement ) { final SqmQueryPart queryPart = selectStatement.getQueryPart(); // For criteria queries, we have to validate the fetch structure here queryPart.validateQueryStructureAndFetchOwners(); @@ -464,13 +463,13 @@ public class QuerySqmImpl private SelectQueryPlan 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 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 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 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 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 private NonSelectQueryPlan buildUpdateQueryPlan() { final SqmUpdateStatement sqmUpdate = (SqmUpdateStatement) 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 private NonSelectQueryPlan buildInsertQueryPlan() { final SqmInsertStatement sqmInsert = (SqmInsertStatement) 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 persister.getSqmMultiTableInsertStrategy() ); } - else if ( sqmInsert instanceof SqmInsertValuesStatement - && ( (SqmInsertValuesStatement) sqmInsert ).getValuesList().size() != 1 + else if ( sqmInsert instanceof SqmInsertValuesStatement insertValues + && insertValues.getValuesList().size() != 1 && !getSessionFactory().getJdbcServices().getDialect().supportsValuesListForInsert() ) { // Split insert-values queries if the dialect doesn't support values lists - final SqmInsertValuesStatement insertValues = (SqmInsertValuesStatement) sqmInsert; final List 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,13 +648,10 @@ public class QuerySqmImpl ? 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 ) ) { - return true; + if ( insertionTargetPath.getLhs() instanceof SqmRoot ) { + if ( insertionTargetPath.getReferencedPathSource().getPathName().equals( partName ) ) { + return true; + } } }