diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/NamedNativeQuery.java b/hibernate-core/src/main/java/org/hibernate/annotations/NamedNativeQuery.java index 4e1538de9a..8607dcb6d3 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/NamedNativeQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/NamedNativeQuery.java @@ -132,6 +132,14 @@ public @interface NamedNativeQuery { */ CacheRetrieveMode cacheRetrieveMode() default CacheRetrieveMode.USE; + /** + * The cache interaction mode for this query. + * + * @see org.hibernate.query.SelectionQuery#setCacheMode(CacheMode) + * @see org.hibernate.jpa.HibernateHints#HINT_CACHE_MODE + */ + CacheMode cacheMode() default CacheMode.NORMAL; + /** * Whether the results should be loaded in read-only mode. * Default is {@code false}. diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/NamedQuery.java b/hibernate-core/src/main/java/org/hibernate/annotations/NamedQuery.java index 09d841fa6a..61c8bba6f3 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/NamedQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/NamedQuery.java @@ -14,7 +14,7 @@ import jakarta.persistence.CacheRetrieveMode; import jakarta.persistence.CacheStoreMode; import jakarta.persistence.EntityManager; -import org.hibernate.Remove; +import org.hibernate.CacheMode; import static java.lang.annotation.ElementType.PACKAGE; import static java.lang.annotation.ElementType.TYPE; @@ -126,6 +126,14 @@ public @interface NamedQuery { */ CacheRetrieveMode cacheRetrieveMode() default CacheRetrieveMode.USE; + /** + * The cache interaction mode for this query. + * + * @see org.hibernate.query.SelectionQuery#setCacheMode(CacheMode) + * @see org.hibernate.jpa.HibernateHints#HINT_CACHE_MODE + */ + CacheMode cacheMode() default CacheMode.NORMAL; + /** * Whether the results should be loaded in read-only mode. * Default is {@code false}. diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedNativeQueryAnnotation.java b/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedNativeQueryAnnotation.java index e2bdd94561..dd6dae1fdf 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedNativeQueryAnnotation.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedNativeQueryAnnotation.java @@ -10,6 +10,7 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; +import org.hibernate.CacheMode; import org.hibernate.annotations.FlushModeType; import org.hibernate.annotations.NamedNativeQuery; import org.hibernate.boot.jaxb.mapping.spi.JaxbNamedNativeQueryImpl; @@ -76,6 +77,10 @@ public class NamedNativeQueryAnnotation implements NamedNativeQuery { this.comment = annotation.comment(); this.cacheStoreMode = annotation.cacheStoreMode(); this.cacheRetrieveMode = annotation.cacheRetrieveMode(); + if ( annotation.cacheMode() != CacheMode.NORMAL ) { + this.cacheStoreMode = annotation.cacheMode().getJpaStoreMode(); + this.cacheRetrieveMode = annotation.cacheMode().getJpaRetrieveMode(); + } this.readOnly = annotation.readOnly(); this.querySpaces = annotation.querySpaces(); this.callable = annotation.callable(); @@ -214,6 +219,11 @@ public class NamedNativeQueryAnnotation implements NamedNativeQuery { this.cacheRetrieveMode = value; } + @Override + public CacheMode cacheMode() { + return CacheMode.fromJpaModes( cacheRetrieveMode, cacheStoreMode ); + } + @Override public boolean readOnly() { return readOnly; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedQueryAnnotation.java b/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedQueryAnnotation.java index 8e4dd43ae8..592c8a09ab 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedQueryAnnotation.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/NamedQueryAnnotation.java @@ -8,6 +8,7 @@ package org.hibernate.boot.models.annotations.internal; import java.lang.annotation.Annotation; +import org.hibernate.CacheMode; import org.hibernate.annotations.FlushModeType; import org.hibernate.annotations.NamedQuery; import org.hibernate.boot.jaxb.mapping.spi.JaxbNamedQueryImpl; @@ -65,6 +66,10 @@ public class NamedQueryAnnotation implements NamedQuery { this.comment = annotation.comment(); this.cacheStoreMode = annotation.cacheStoreMode(); this.cacheRetrieveMode = annotation.cacheRetrieveMode(); + if ( annotation.cacheMode() != CacheMode.NORMAL ) { + this.cacheStoreMode = annotation.cacheMode().getJpaStoreMode(); + this.cacheRetrieveMode = annotation.cacheMode().getJpaRetrieveMode(); + } this.readOnly = annotation.readOnly(); } @@ -189,6 +194,11 @@ public class NamedQueryAnnotation implements NamedQuery { this.cacheRetrieveMode = value; } + @Override + public CacheMode cacheMode() { + return CacheMode.fromJpaModes( cacheRetrieveMode, cacheStoreMode ); + } + @Override public boolean readOnly() { return readOnly;