From cd425e3502dfde6bf5e5516aa486e6347f8fc03f Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 9 Nov 2022 10:05:51 +0100 Subject: [PATCH] HHH-15691 add @Cache(includeLazy) and deprecate stringly-typed include member + add some javadoc --- .../src/main/java/org/hibernate/Cache.java | 2 ++ .../java/org/hibernate/annotations/Cache.java | 28 +++++++++++++++++-- .../org/hibernate/annotations/LazyGroup.java | 2 ++ .../cfg/annotations/EntityBinder.java | 8 ++++++ .../test/type/LobUnfetchedPropertyTest.java | 6 ++-- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Cache.java b/hibernate-core/src/main/java/org/hibernate/Cache.java index a3ec61067d..26df24763d 100644 --- a/hibernate-core/src/main/java/org/hibernate/Cache.java +++ b/hibernate-core/src/main/java/org/hibernate/Cache.java @@ -62,6 +62,8 @@ package org.hibernate; * current transaction and/or locking scheme. * * @author Steve Ebersole + * + * @see org.hibernate.annotations.Cache */ public interface Cache extends jakarta.persistence.Cache { /** diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Cache.java b/hibernate-core/src/main/java/org/hibernate/annotations/Cache.java index 0b0be2cba5..964582c4a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Cache.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Cache.java @@ -5,6 +5,7 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.annotations; + import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -33,6 +34,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; * cache inherit the cache belonging to the root entity. * * @see jakarta.persistence.Cacheable + * @see org.hibernate.Cache * * @author Emmanuel Bernard */ @@ -51,12 +53,34 @@ public @interface Cache { String region() default ""; /** - * Specifies which properties are included in the second-level - * cache, either: + * When bytecode enhancement is used, and {@linkplain LazyGroup + * field-level lazy fetching} is enabled, specifies whether lazy + * attributes of the entity are eligible for inclusion in the + * second-level cache, in the case where they happen to be loaded. + *

+ * By default, a loaded lazy field will be cached when + * second-level caching is enabled. If this is not desirable—if, + * for example, the field value is extremely large and only rarely + * accessed—then setting {@code @Cache(includeLazy=false)} will + * prevent it and other lazy fields of the annotated entity from being + * cached, and the lazy fields will always be retrieved directly from + * the database. + * + * @see LazyGroup + */ + boolean includeLazy() default true; + + /** + * When bytecode enhancement is used, and {@linkplain LazyGroup + * field-level lazy fetching} is enabled, specifies which attributes + * of the entity are included in the second-level cache, either: *

+ * + * @deprecated Use {@link #includeLazy()} for the sake of typesafety. */ + @Deprecated String include() default "all"; } diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/LazyGroup.java b/hibernate-core/src/main/java/org/hibernate/annotations/LazyGroup.java index 4424f594d6..1a1f713129 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/LazyGroup.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/LazyGroup.java @@ -41,6 +41,8 @@ import java.lang.annotation.RetentionPolicy; * projects using Hibernate don't even bother enabling the bytecode enhancer. * * @author Steve Ebersole + * + * @see Cache#includeLazy() */ @java.lang.annotation.Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 4e43221d6e..2411b3b7fe 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -1436,6 +1436,9 @@ public class EntityBinder { } private static boolean isCacheLazy(Cache effectiveCache, XClass annotatedClass) { + if ( !effectiveCache.includeLazy() ) { + return false; + } switch ( effectiveCache.include().toLowerCase( Locale.ROOT ) ) { case "all": return true; @@ -1490,6 +1493,11 @@ public class EntityBinder { return region; } + @Override + public boolean includeLazy() { + return true; + } + public String include() { return "all"; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java index 1ae945bb9d..ab9174de59 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java @@ -131,7 +131,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { } @Entity(name = "FileBlob") - @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy") + @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = false) public static class FileBlob { private int id; @@ -161,7 +161,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { } @Entity(name = "FileClob") - @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy") + @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = false) public static class FileClob { private int id; @@ -191,7 +191,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { } @Entity(name = "FileNClob") - @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy") + @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = false) public static class FileNClob { private int id;