diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/MutableQueryOptions.java b/hibernate-core/src/main/java/org/hibernate/query/spi/MutableQueryOptions.java index 8f543877e8..551f811924 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/MutableQueryOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/MutableQueryOptions.java @@ -11,6 +11,7 @@ import javax.persistence.CacheStoreMode; import org.hibernate.CacheMode; import org.hibernate.FlushMode; +import org.hibernate.query.QueryLogging; import org.hibernate.query.ResultListTransformer; import org.hibernate.query.TupleTransformer; @@ -37,6 +38,11 @@ public interface MutableQueryOptions extends QueryOptions { * Corollary to {@link #getCacheMode()} */ default void setCacheMode(CacheMode cacheMode) { + if ( cacheMode == null ) { + QueryLogging.QUERY_LOGGER.debug( "Null CacheMode passed to #setCacheMode; falling back to `NORMAL`" ); + cacheMode = CacheMode.NORMAL; + } + setCacheRetrieveMode( cacheMode.getJpaRetrieveMode() ); setCacheStoreMode( cacheMode.getJpaStoreMode() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/options/CacheModeTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/options/CacheModeTests.java new file mode 100644 index 0000000000..506db4b84d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/options/CacheModeTests.java @@ -0,0 +1,30 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.query.options; + +import org.hibernate.testing.orm.domain.StandardDomainModel; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +/** + * @author Steve Ebersole + */ +@DomainModel( standardModels = StandardDomainModel.CONTACTS ) +@SessionFactory +public class CacheModeTests { + @Test + public void testNullCacheMode(SessionFactoryScope scope) { + // tests passing null as CacheMode + scope.inTransaction( (session) -> { + session.createQuery( "select c from Contact c" ) + .setCacheMode( null ) + .list(); + }); + } +}