From 778675b8678bcb8a6ca2f65188c1d3741e83dac6 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 10 Nov 2022 10:15:17 +0100 Subject: [PATCH] HHH-15702 add Session.setCacheRetrieveMode, Session.setCacheStoreMode --- .../src/main/java/org/hibernate/Session.java | 20 +++++++++++++++++++ .../engine/spi/SessionDelegatorBaseImpl.java | 12 +++++++++++ .../engine/spi/SessionLazyDelegator.java | 14 ++++++++++++- .../org/hibernate/internal/SessionImpl.java | 17 +++++++++++++--- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Session.java b/hibernate-core/src/main/java/org/hibernate/Session.java index 8bbabdca91..43d21195a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/Session.java +++ b/hibernate-core/src/main/java/org/hibernate/Session.java @@ -8,6 +8,8 @@ package org.hibernate; import java.util.List; +import jakarta.persistence.CacheRetrieveMode; +import jakarta.persistence.CacheStoreMode; import jakarta.persistence.PessimisticLockScope; import org.hibernate.graph.RootGraph; import org.hibernate.query.Query; @@ -203,6 +205,24 @@ public interface Session extends SharedSessionContract, EntityManager { */ CacheMode getCacheMode(); + /** + * Enable or disable writes to the second-level cache. + * + * @param cacheStoreMode a JPA-defined {@link CacheStoreMode} + * + * @see #setCacheMode(CacheMode) + */ + void setCacheStoreMode(CacheStoreMode cacheStoreMode); + + /** + * Enable or disable reads from the second-level cache. + * + * @param cacheRetrieveMode a JPA-defined {@link CacheRetrieveMode} + * + * @see #setCacheMode(CacheMode) + */ + void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode); + /** * Get the session factory which created this session. * diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 79ba052139..3be5022103 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -12,6 +12,8 @@ import java.util.Set; import java.util.TimeZone; import java.util.UUID; +import jakarta.persistence.CacheRetrieveMode; +import jakarta.persistence.CacheStoreMode; import org.hibernate.CacheMode; import org.hibernate.Filter; import org.hibernate.FlushMode; @@ -205,6 +207,16 @@ public class SessionDelegatorBaseImpl implements SessionImplementor { delegate.setCacheMode( cm ); } + @Override + public void setCacheStoreMode(CacheStoreMode cacheStoreMode) { + delegate.setCacheStoreMode( cacheStoreMode ); + } + + @Override + public void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) { + delegate.setCacheRetrieveMode( cacheRetrieveMode ); + } + @Override public void setCriteriaCopyTreeEnabled(boolean jpaCriteriaCopyComplianceEnabled) { delegate.setCriteriaCopyTreeEnabled( jpaCriteriaCopyComplianceEnabled ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java index a591143dbd..65d44606f0 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java @@ -10,6 +10,8 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import jakarta.persistence.CacheRetrieveMode; +import jakarta.persistence.CacheStoreMode; import org.hibernate.CacheMode; import org.hibernate.Filter; import org.hibernate.FlushMode; @@ -99,6 +101,16 @@ public class SessionLazyDelegator implements Session { this.lazySession.get().setCacheMode( cacheMode ); } + @Override + public void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) { + this.lazySession.get().setCacheRetrieveMode( cacheRetrieveMode ); + } + + @Override + public void setCacheStoreMode(CacheStoreMode cacheStoreMode) { + this.lazySession.get().setCacheStoreMode( cacheStoreMode ); + } + @Override public CacheMode getCacheMode() { return this.lazySession.get().getCacheMode(); @@ -275,7 +287,7 @@ public class SessionLazyDelegator implements Session { this.lazySession.get().lock( object, lockMode ); } - @Override + @Override @Deprecated public void lock(String entityName, Object object, LockMode lockMode) { this.lazySession.get().lock( entityName, object, lockMode ); } 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 c88698e95f..a6451a90c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -152,6 +152,7 @@ import jakarta.persistence.PersistenceException; import jakarta.persistence.TransactionRequiredException; import org.hibernate.type.descriptor.WrapperOptions; +import static org.hibernate.CacheMode.fromJpaModes; import static org.hibernate.cfg.AvailableSettings.JAKARTA_LOCK_SCOPE; import static org.hibernate.cfg.AvailableSettings.JAKARTA_LOCK_TIMEOUT; import static org.hibernate.cfg.AvailableSettings.JAKARTA_SHARED_CACHE_RETRIEVE_MODE; @@ -1835,6 +1836,16 @@ public class SessionImpl persistenceContext.setReadOnly( entity, readOnly ); } + @Override + public void setCacheStoreMode(CacheStoreMode cacheStoreMode) { + setCacheMode( fromJpaModes( getCacheMode().getJpaRetrieveMode(), cacheStoreMode ) ); + } + + @Override + public void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) { + setCacheMode( fromJpaModes( cacheRetrieveMode, getCacheMode().getJpaStoreMode() ) ); + } + @Override public void afterScrollOperation() { // nothing to do in a stateful session @@ -2119,18 +2130,18 @@ public class SessionImpl return this; } - @Override + @Override @Deprecated @SuppressWarnings("deprecation") public boolean getScope() { return lockOptions.getScope(); } - @Override + @Override @Deprecated @SuppressWarnings("deprecation") public LockRequest setScope(boolean scope) { lockOptions.setScope( scope ); return this; } - @Override + @Override @Deprecated @SuppressWarnings("deprecation") public void lock(String entityName, Object object) throws HibernateException { fireLock( entityName, object, lockOptions ); }