HHH-15702 add SelectionQuery.setCacheRetrieveMode, SelectionQuery.setCacheStoreMode

also clean up some unnecessary overriding in Query hierarchy
This commit is contained in:
Gavin King 2022-11-10 09:58:16 +01:00
parent a56a7c523b
commit 5c90779a02
8 changed files with 124 additions and 58 deletions

View File

@ -13,6 +13,8 @@ import java.util.Date;
import java.util.Map;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter;
@ -523,6 +525,12 @@ public interface NativeQuery<T> extends Query<T>, SynchronizeableQuery {
@Override
NativeQuery<T> setCacheMode(CacheMode cacheMode);
@Override
NativeQuery<T> setCacheStoreMode(CacheStoreMode cacheStoreMode);
@Override
NativeQuery<T> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode);
@Override
NativeQuery<T> setCacheable(boolean cacheable);

View File

@ -15,6 +15,8 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Incubating;
@ -852,6 +854,12 @@ public interface Query<R> extends SelectionQuery<R>, MutationQuery, TypedQuery<R
@Override
Query<R> setCacheMode(CacheMode cacheMode);
@Override
Query<R> setCacheStoreMode(CacheStoreMode cacheStoreMode);
@Override
Query<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode);
@Override
Query<R> setTimeout(int timeout);

View File

@ -15,6 +15,8 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Incubating;
@ -270,14 +272,14 @@ public interface SelectionQuery<R> extends CommonQueryContract {
* the query inherits the {@link CacheMode} of the session from which
* it originates.
* <p/>
* NOTE: The {@link CacheMode} here describes reading-from/writing-to the
* entity/collection caches as we process query results. For caching of
* the actual query results, see {@link #isCacheable()} and
* NOTE: The {@link CacheMode} here describes reading-from/writing-to
* the entity/collection caches as we process query results. For caching
* of the actual query results, see {@link #isCacheable()} and
* {@link #getCacheRegion()}
* <p/>
* In order for this setting to have any affect, second-level caching would
* have to be enabled and the entities/collections in question configured
* for caching.
* In order for this setting to have any affect, second-level caching
* would have to be enabled and the entities/collections in question
* configured for caching.
*
* @see Session#getCacheMode()
*/
@ -294,6 +296,16 @@ public interface SelectionQuery<R> extends CommonQueryContract {
*/
SelectionQuery<R> setCacheMode(CacheMode cacheMode);
/**
* @see #setCacheMode(CacheMode)
*/
SelectionQuery<R> setCacheStoreMode(CacheStoreMode cacheStoreMode);
/**
* @see #setCacheMode(CacheMode)
*/
SelectionQuery<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode);
/**
* Should the results of the query be stored in the second level cache?
* <p/>
@ -379,7 +391,7 @@ public interface SelectionQuery<R> extends CommonQueryContract {
SelectionQuery<R> setAliasSpecificLockMode(String alias, LockMode lockMode);
/**
* Specifies whether follow-on locking should be applied?
* Specifies whether follow-on locking should be applied
*/
SelectionQuery<R> setFollowOnLocking(boolean enable);

View File

@ -13,6 +13,9 @@ import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter;
@ -204,6 +207,19 @@ public abstract class AbstractQuery<R>
return this;
}
@Override
public QueryImplementor<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
super.setCacheRetrieveMode( cacheRetrieveMode );
return this;
}
@Override
public QueryImplementor<R> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
super.setCacheStoreMode( cacheStoreMode );
return this;
}
@Override
public boolean isCacheable() {
return super.isCacheable();
@ -258,7 +274,7 @@ public abstract class AbstractQuery<R>
@Override
public QueryImplementor<R> setLockMode(String alias, LockMode lockMode) {
getQueryOptions().getLockOptions().setAliasSpecificLockMode( alias, lockMode );
super.setLockMode( alias, lockMode );
return this;
}

View File

@ -19,6 +19,9 @@ import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.NoResultException;
@ -69,6 +72,7 @@ import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.PrimitiveJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import static org.hibernate.CacheMode.fromJpaModes;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_SHARED_CACHE_RETRIEVE_MODE;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_SHARED_CACHE_STORE_MODE;
import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE;
@ -581,6 +585,12 @@ public abstract class AbstractSelectionQuery<R>
return this;
}
@Override
public SelectionQuery<R> setLockMode(String alias, LockMode lockMode) {
getQueryOptions().getLockOptions().setAliasSpecificLockMode( alias, lockMode );
return this;
}
/**
* Get the root LockMode for the query
*/
@ -682,6 +692,16 @@ public abstract class AbstractSelectionQuery<R>
return this;
}
@Override
public SelectionQuery<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
return setCacheMode( fromJpaModes( cacheRetrieveMode, getQueryOptions().getCacheMode().getJpaStoreMode() ) );
}
@Override
public SelectionQuery<R> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
return setCacheMode( fromJpaModes( getQueryOptions().getCacheMode().getJpaRetrieveMode(), cacheStoreMode ) );
}
@Override
public boolean isCacheable() {
return getQueryOptions().isResultCachingEnabled() == Boolean.TRUE;

View File

@ -20,6 +20,8 @@ import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
@ -1111,6 +1113,18 @@ public class NativeQueryImpl<R>
return this;
}
@Override
public NativeQueryImplementor<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
super.setCacheRetrieveMode( cacheRetrieveMode );
return this;
}
@Override
public NativeQueryImplementor<R> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
super.setCacheStoreMode( cacheStoreMode );
return this;
}
@Override
public NativeQueryImplementor<R> setCacheable(boolean cacheable) {
super.setCacheable( cacheable );

View File

@ -8,7 +8,6 @@ package org.hibernate.query.sqm.internal;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
@ -19,6 +18,9 @@ import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter;
@ -47,7 +49,6 @@ import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.enhanced.Optimizer;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.collections.IdentitySet;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping;
import org.hibernate.metamodel.model.domain.EntityDomainType;
@ -85,7 +86,6 @@ import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.query.spi.SelectQueryPlan;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmStatement;
@ -912,7 +912,7 @@ public class QuerySqmImpl<R>
@Override
public SqmQueryImplementor<R> setLockMode(String alias, LockMode lockMode) {
// No verifySelect call, because in Hibernate we support locking in subqueries
getQueryOptions().getLockOptions().setAliasSpecificLockMode( alias, lockMode );
super.setLockMode( alias, lockMode );
return this;
}
@ -1216,6 +1216,18 @@ public class QuerySqmImpl<R>
return this;
}
@Override
public SqmQueryImplementor<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
super.setCacheRetrieveMode( cacheRetrieveMode );
return this;
}
@Override
public SqmQueryImplementor<R> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
super.setCacheStoreMode( cacheStoreMode );
return this;
}
@Override
public SqmQueryImplementor<R> setCacheable(boolean cacheable) {
super.setCacheable( cacheable );

View File

@ -14,6 +14,9 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter;
@ -24,15 +27,12 @@ import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.graph.spi.AppliedGraph;
import org.hibernate.internal.util.collections.IdentitySet;
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.query.BindableType;
import org.hibernate.query.QueryLogging;
import org.hibernate.query.QueryParameter;
@ -428,22 +428,12 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
return false;
}
@Override
public FlushModeType getFlushMode() {
return FlushModeTypeHelper.getFlushModeType( getQueryOptions().getFlushMode() );
}
@Override
public SqmSelectionQuery<R> setFlushMode(FlushModeType flushMode) {
setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushMode ) );
super.setFlushMode( flushMode );
return this;
}
@Override
public LockOptions getLockOptions() {
return getQueryOptions().getLockOptions();
}
/**
* Specify the root LockModeType for the query
*
@ -451,7 +441,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
*/
@Override
public SqmSelectionQuery<R> setLockMode(LockModeType lockMode) {
setHibernateLockMode( LockModeTypeHelper.getLockMode( lockMode ) );
super.setLockMode( lockMode );
return this;
}
@ -460,7 +450,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
*/
@Override
public SqmSelectionQuery<R> setHibernateLockMode(LockMode lockMode) {
getLockOptions().setLockMode( lockMode );
super.setHibernateLockMode( lockMode );
return this;
}
@ -471,7 +461,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
*/
@Override @Deprecated
public SqmSelectionQuery<R> setAliasSpecificLockMode(String alias, LockMode lockMode) {
getLockOptions().setAliasSpecificLockMode( alias, lockMode );
super.setAliasSpecificLockMode( alias, lockMode );
return this;
}
@ -480,7 +470,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
*/
@Override
public SqmSelectionQuery<R> setLockMode(String alias, LockMode lockMode) {
getLockOptions().setAliasSpecificLockMode( alias, lockMode );
super.setLockMode( alias, lockMode );
return this;
}
@ -493,59 +483,45 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
return this;
}
@Override
public Integer getFetchSize() {
return getQueryOptions().getFetchSize();
}
@Override
public SqmSelectionQuery<R> setFetchSize(int fetchSize) {
getQueryOptions().setFetchSize( fetchSize );
super.setFetchSize( fetchSize );
return this;
}
@Override
public boolean isReadOnly() {
return getQueryOptions().isReadOnly() == null
? getSession().isDefaultReadOnly()
: getQueryOptions().isReadOnly();
}
@Override
public SqmSelectionQuery<R> setReadOnly(boolean readOnly) {
getQueryOptions().setReadOnly( readOnly );
super.setReadOnly( readOnly );
return this;
}
@Override
public CacheMode getCacheMode() {
return getQueryOptions().getCacheMode();
}
@Override
public SqmSelectionQuery<R> setCacheMode(CacheMode cacheMode) {
getQueryOptions().setCacheMode( cacheMode );
super.setCacheMode( cacheMode );
return this;
}
@Override
public boolean isCacheable() {
return getQueryOptions().isResultCachingEnabled() == Boolean.TRUE;
public SqmSelectionQuery<R> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
super.setCacheRetrieveMode( cacheRetrieveMode );
return this;
}
@Override
public SqmSelectionQuery<R> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
super.setCacheStoreMode( cacheStoreMode );
return this;
}
@Override
public SqmSelectionQuery<R> setCacheable(boolean cacheable) {
getQueryOptions().setResultCachingEnabled( cacheable );
super.setCacheable( cacheable );
return this;
}
@Override
public String getCacheRegion() {
return getQueryOptions().getResultCacheRegionName();
}
@Override
public SqmSelectionQuery<R> setCacheRegion(String regionName) {
getQueryOptions().setResultCacheRegionName( regionName );
super.setCacheRegion( regionName );
return this;
}