Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -76,6 +77,7 @@
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;
Expand All @@ -85,7 +87,6 @@
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;
Expand Down Expand Up @@ -122,6 +123,7 @@
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
Expand Down Expand Up @@ -858,11 +860,11 @@ public <R> SelectionQuery<R> createSelectionQuery(String hqlString, Class<R> exp

@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 );
}
}
Expand Down Expand Up @@ -940,8 +942,7 @@ public NativeQueryImplementor createNativeQuery(String sqlString, String resultS

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 );
Expand Down Expand Up @@ -1048,14 +1049,16 @@ public <R> SelectionQuery<R> createNamedSelectionQuery(String queryName, Class<R
);
}

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(
Expand All @@ -1068,15 +1071,12 @@ private <R> SqmSelectionQuery<R> createNamedSqmSelectionQuery(
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;
}
Expand Down Expand Up @@ -1426,8 +1426,8 @@ public HibernateCriteriaBuilder getCriteriaBuilder() {
@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1655,8 +1655,8 @@ public SessionFactoryImplementor getSessionFactory() {
@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,11 @@ public 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<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();
Expand Down Expand Up @@ -464,13 +463,13 @@ public boolean isQueryPlanCacheable() {

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();
}


Expand Down Expand Up @@ -525,8 +524,7 @@ private NonSelectQueryPlan resolveNonSelectQueryPlan() {
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 );
}
Expand All @@ -544,19 +542,19 @@ private NonSelectQueryPlan resolveNonSelectQueryPlan() {
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() {
Expand All @@ -569,8 +567,9 @@ private NonSelectQueryPlan buildDeleteQueryPlan() {

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
Expand All @@ -591,11 +590,9 @@ private NonSelectQueryPlan buildAggregatedDeleteQueryPlan(SqmDeleteStatement<?>[

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 )
Expand All @@ -604,17 +601,16 @@ private NonSelectQueryPlan buildUpdateQueryPlan() {

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 );
}
Expand All @@ -627,15 +623,15 @@ private NonSelectQueryPlan buildInsertQueryPlan() {
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 );
}
Expand All @@ -652,13 +648,10 @@ protected boolean hasIdentifierAssigned(SqmInsertStatement<?> sqmInsert, EntityP
? 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;
}
}
}

Expand Down
Loading