remove order-by list from the query plan cache key

Roll back my always-intended-to-be-temporary bandaid to allow
caching of the query plan after calls to setOrder(). Instead,
just disable caching of the query plan. When the query plan
cache can handle caching of criteria queries, change to use
that strategy instead.
This commit is contained in:
Gavin King 2023-07-18 18:55:59 +02:00
parent e2ec3cd3e7
commit e5371386a4
4 changed files with 17 additions and 25 deletions

View File

@ -950,8 +950,12 @@ public FlushModeType getFlushMode() {
public Query<R> setOrder(List<Order<? super R>> orderList) { public Query<R> setOrder(List<Order<? super R>> orderList) {
if ( sqm instanceof SqmSelectStatement ) { if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm; final SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
select.orderBy( orderList.stream().map( order -> sortSpecification( select, order ) ).collect( toList() ) ); select.orderBy( orderList.stream().map( order -> sortSpecification( select, order ) )
.collect( toList() ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
return this; return this;
} }
else { else {
@ -965,6 +969,9 @@ public Query<R> setOrder(Order<? super R> order) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm; SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
select.orderBy( sortSpecification( select, order ) ); select.orderBy( sortSpecification( select, order ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
return this; return this;
} }
else { else {
@ -972,13 +979,6 @@ public Query<R> setOrder(Order<? super R> order) {
} }
} }
@Override
public List<jakarta.persistence.criteria.Order> getOrder() {
return sqm instanceof SqmSelectStatement
? ((SqmSelectStatement<R>) sqm).getOrderList()
: null;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hints // hints

View File

@ -8,12 +8,10 @@
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import jakarta.persistence.criteria.Order;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.engine.spi.LoadQueryInfluencers; import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.query.ResultListTransformer; import org.hibernate.query.ResultListTransformer;
@ -40,7 +38,6 @@ public interface CacheabilityInfluencers {
public interface InterpretationsKeySource extends CacheabilityInfluencers { public interface InterpretationsKeySource extends CacheabilityInfluencers {
Class<?> getResultType(); Class<?> getResultType();
List<Order> getOrder();
} }
public static SqmInterpretationsKey createInterpretationsKey(InterpretationsKeySource keySource) { public static SqmInterpretationsKey createInterpretationsKey(InterpretationsKeySource keySource) {
@ -52,7 +49,6 @@ public static SqmInterpretationsKey createInterpretationsKey(InterpretationsKeyS
query, query,
query.hashCode(), query.hashCode(),
keySource.getResultType(), keySource.getResultType(),
keySource.getOrder(),
keySource.getQueryOptions().getLockOptions(), keySource.getQueryOptions().getLockOptions(),
keySource.getQueryOptions().getTupleTransformer(), keySource.getQueryOptions().getTupleTransformer(),
keySource.getQueryOptions().getResultListTransformer(), keySource.getQueryOptions().getResultListTransformer(),
@ -113,7 +109,6 @@ public static QueryInterpretationCache.Key generateNonSelectKey(InterpretationsK
private final Object query; private final Object query;
private final Class<?> resultType; private final Class<?> resultType;
private final List<Order> order;
private final LockOptions lockOptions; private final LockOptions lockOptions;
private final TupleTransformer<?> tupleTransformer; private final TupleTransformer<?> tupleTransformer;
private final ResultListTransformer<?> resultListTransformer; private final ResultListTransformer<?> resultListTransformer;
@ -124,7 +119,6 @@ private SqmInterpretationsKey(
Object query, Object query,
int hash, int hash,
Class<?> resultType, Class<?> resultType,
List<Order> order,
LockOptions lockOptions, LockOptions lockOptions,
TupleTransformer<?> tupleTransformer, TupleTransformer<?> tupleTransformer,
ResultListTransformer<?> resultListTransformer, ResultListTransformer<?> resultListTransformer,
@ -132,7 +126,6 @@ private SqmInterpretationsKey(
this.query = query; this.query = query;
this.hashcode = hash; this.hashcode = hash;
this.resultType = resultType; this.resultType = resultType;
this.order = order;
this.lockOptions = lockOptions; this.lockOptions = lockOptions;
this.tupleTransformer = tupleTransformer; this.tupleTransformer = tupleTransformer;
this.resultListTransformer = resultListTransformer; this.resultListTransformer = resultListTransformer;
@ -145,7 +138,6 @@ public QueryInterpretationCache.Key prepareForStore() {
query, query,
hashcode, hashcode,
resultType, resultType,
order,
// Since lock options might be mutable, we need a copy for the cache key // Since lock options might be mutable, we need a copy for the cache key
lockOptions.makeDefensiveCopy(), lockOptions.makeDefensiveCopy(),
tupleTransformer, tupleTransformer,
@ -172,7 +164,6 @@ public boolean equals(Object o) {
return this.hashcode == o.hashCode() //check this first as some other checks are expensive return this.hashcode == o.hashCode() //check this first as some other checks are expensive
&& query.equals( that.query ) && query.equals( that.query )
&& Objects.equals( resultType, that.resultType ) && Objects.equals( resultType, that.resultType )
&& Objects.equals( order, that.order )
&& Objects.equals( lockOptions, that.lockOptions ) && Objects.equals( lockOptions, that.lockOptions )
&& Objects.equals( tupleTransformer, that.tupleTransformer ) && Objects.equals( tupleTransformer, that.tupleTransformer )
&& Objects.equals( resultListTransformer, that.resultListTransformer ) && Objects.equals( resultListTransformer, that.resultListTransformer )

View File

@ -293,7 +293,11 @@ public SelectionQuery<R> setPage(Page page) {
@Override @Override
public final SelectionQuery<R> setOrder(List<Order<? super R>> orderList) { public final SelectionQuery<R> setOrder(List<Order<? super R>> orderList) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
sqm.orderBy( orderList.stream().map( order -> sortSpecification( sqm, order ) ).collect( toList() ) ); sqm.orderBy( orderList.stream().map( order -> sortSpecification( sqm, order ) )
.collect( toList() ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
return this; return this;
} }
@ -301,6 +305,9 @@ public final SelectionQuery<R> setOrder(List<Order<? super R>> orderList) {
public final SelectionQuery<R> setOrder(Order<? super R> order) { public final SelectionQuery<R> setOrder(Order<? super R> order) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
sqm.orderBy( sortSpecification( sqm, order ) ); sqm.orderBy( sortSpecification( sqm, order ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
return this; return this;
} }
@ -584,11 +591,6 @@ public boolean isQueryPlanCacheable() {
: super.isQueryPlanCacheable(); : super.isQueryPlanCacheable();
} }
@Override
public List<jakarta.persistence.criteria.Order> getOrder() {
return sqm.getOrderList();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hints // hints

View File

@ -12,7 +12,6 @@
import java.util.List; import java.util.List;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.hibernate.query.Order.asc; import static org.hibernate.query.Order.asc;
import static org.hibernate.query.Order.desc; import static org.hibernate.query.Order.desc;