Skip to content

Commit 2a4f430

Browse files
committed
remove deprecated and very ugly member of @Cache
1 parent 05852b7 commit 2a4f430

File tree

5 files changed

+39
-87
lines changed

5 files changed

+39
-87
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/Cache.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,4 @@
8989
* @see LazyGroup
9090
*/
9191
boolean includeLazy() default true;
92-
93-
/**
94-
* When bytecode enhancement is used, and {@linkplain LazyGroup
95-
* field-level lazy fetching} is enabled, specifies which attributes
96-
* of the entity are included in the second-level cache, either:
97-
* <ul>
98-
* <li>{@code "all"} properties, the default, or
99-
* <li>only {@code "non-lazy"} properties.
100-
* </ul>
101-
*
102-
* @deprecated Use {@link #includeLazy()} for the sake of typesafety.
103-
*/
104-
@Deprecated(since="6.4")
105-
String include() default "all";
10692
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.HashSet;
1212
import java.util.Iterator;
1313
import java.util.List;
14-
import java.util.Locale;
1514
import java.util.Map;
1615
import java.util.Set;
1716

@@ -127,7 +126,6 @@
127126
import jakarta.persistence.PrimaryKeyJoinColumns;
128127
import jakarta.persistence.SecondaryTable;
129128
import jakarta.persistence.SecondaryTables;
130-
import jakarta.persistence.SharedCacheMode;
131129
import jakarta.persistence.UniqueConstraint;
132130

133131
import static jakarta.persistence.InheritanceType.SINGLE_TABLE;
@@ -1639,24 +1637,24 @@ public void setWrapIdsInEmbeddedComponents(boolean wrapIdsInEmbeddedComponents)
16391637
}
16401638

16411639
private void bindNaturalIdCache() {
1642-
naturalIdCacheRegion = null;
16431640
final NaturalIdCache naturalIdCacheAnn =
16441641
annotatedClass.getAnnotationUsage( NaturalIdCache.class, getSourceModelContext() );
1645-
if ( naturalIdCacheAnn == null ) {
1646-
return;
1647-
}
1648-
1649-
final String region = naturalIdCacheAnn.region();
1650-
if ( region.isBlank() ) {
1651-
final Cache explicitCacheAnn = annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
1652-
1653-
naturalIdCacheRegion =
1654-
explicitCacheAnn != null && isNotBlank( explicitCacheAnn.region() )
1655-
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
1656-
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
1642+
if ( naturalIdCacheAnn != null ) {
1643+
final String region = naturalIdCacheAnn.region();
1644+
if ( region.isBlank() ) {
1645+
final Cache explicitCacheAnn =
1646+
annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
1647+
naturalIdCacheRegion =
1648+
explicitCacheAnn != null && isNotBlank( explicitCacheAnn.region() )
1649+
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
1650+
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
1651+
}
1652+
else {
1653+
naturalIdCacheRegion = naturalIdCacheAnn.region();
1654+
}
16571655
}
16581656
else {
1659-
naturalIdCacheRegion = naturalIdCacheAnn.region();
1657+
naturalIdCacheRegion = null;
16601658
}
16611659
}
16621660

@@ -1666,16 +1664,15 @@ private void bindCache() {
16661664
cacheRegion = null;
16671665
cacheLazyProperty = true;
16681666
queryCacheLayout = null;
1669-
final SharedCacheMode sharedCacheMode = context.getBuildingOptions().getSharedCacheMode();
1670-
if ( persistentClass instanceof RootClass ) {
1671-
bindRootClassCache( sharedCacheMode, context );
1667+
if ( isRootEntity() ) {
1668+
bindRootClassCache();
16721669
}
16731670
else {
1674-
bindSubclassCache( sharedCacheMode );
1671+
bindSubclassCache();
16751672
}
16761673
}
16771674

1678-
private void bindSubclassCache(SharedCacheMode sharedCacheMode) {
1675+
private void bindSubclassCache() {
16791676
if ( annotatedClass.hasAnnotationUsage( Cache.class, getSourceModelContext() ) ) {
16801677
final String className = persistentClass.getClassName() == null
16811678
? annotatedClass.getName()
@@ -1691,46 +1688,30 @@ private void bindSubclassCache(SharedCacheMode sharedCacheMode) {
16911688
? persistentClass.getSuperclass().isCached()
16921689
//TODO: is this even correct?
16931690
// Do we even correctly support selectively enabling caching on subclasses like this?
1694-
: isCacheable( sharedCacheMode, cacheable );
1691+
: isCacheable( cacheable );
16951692
}
16961693

1697-
private void bindRootClassCache(SharedCacheMode sharedCacheMode, MetadataBuildingContext context) {
1698-
final Cache cache = annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
1699-
final Cacheable cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, getSourceModelContext() );
1700-
final Cache effectiveCache;
1701-
if ( cache != null ) {
1702-
// preserve legacy behavior of circumventing SharedCacheMode when Hibernate's @Cache is used.
1703-
isCached = true;
1704-
effectiveCache = cache;
1705-
}
1706-
else {
1707-
effectiveCache = buildCacheMock( annotatedClass, context );
1708-
isCached = isCacheable( sharedCacheMode, cacheable );
1709-
}
1694+
private void bindRootClassCache() {
1695+
final SourceModelBuildingContext sourceModelContext = getSourceModelContext();
1696+
1697+
final Cache cache = annotatedClass.getAnnotationUsage( Cache.class, sourceModelContext );
1698+
final Cacheable cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, sourceModelContext );
1699+
1700+
// preserve legacy behavior of circumventing SharedCacheMode when Hibernate @Cache is used
1701+
final Cache effectiveCache = cache != null ? cache : buildCacheMock( annotatedClass );
1702+
isCached = cache != null || isCacheable( cacheable );
1703+
17101704
cacheConcurrentStrategy = getCacheConcurrencyStrategy( effectiveCache.usage() );
17111705
cacheRegion = effectiveCache.region();
1712-
cacheLazyProperty = isCacheLazy( effectiveCache, annotatedClass );
1706+
cacheLazyProperty = effectiveCache.includeLazy();
17131707

17141708
final QueryCacheLayout queryCache =
1715-
annotatedClass.getAnnotationUsage( QueryCacheLayout.class, getSourceModelContext() );
1709+
annotatedClass.getAnnotationUsage( QueryCacheLayout.class, sourceModelContext );
17161710
queryCacheLayout = queryCache == null ? null : queryCache.layout();
17171711
}
17181712

1719-
private static boolean isCacheLazy(Cache effectiveCache, ClassDetails annotatedClass) {
1720-
if ( !effectiveCache.includeLazy() ) {
1721-
return false;
1722-
}
1723-
return switch ( effectiveCache.include().toLowerCase( Locale.ROOT ) ) {
1724-
case "all" -> true;
1725-
case "non-lazy" -> false;
1726-
default -> throw new AnnotationException(
1727-
"Class '" + annotatedClass.getName()
1728-
+ "' has a '@Cache' with undefined option 'include=\"" + effectiveCache.include() + "\"'" );
1729-
};
1730-
}
1731-
1732-
private static boolean isCacheable(SharedCacheMode sharedCacheMode, Cacheable explicitCacheableAnn) {
1733-
return switch ( sharedCacheMode ) {
1713+
private boolean isCacheable(Cacheable explicitCacheableAnn) {
1714+
return switch ( context.getBuildingOptions().getSharedCacheMode() ) {
17341715
case ALL ->
17351716
// all entities should be cached
17361717
true;
@@ -1747,15 +1728,15 @@ private static boolean isCacheable(SharedCacheMode sharedCacheMode, Cacheable ex
17471728
};
17481729
}
17491730

1750-
private static Cache buildCacheMock(ClassDetails classDetails, MetadataBuildingContext context) {
1731+
private Cache buildCacheMock(ClassDetails classDetails) {
17511732
final CacheAnnotation cacheUsage =
1752-
HibernateAnnotations.CACHE.createUsage( context.getMetadataCollector().getSourceModelBuildingContext() );
1733+
HibernateAnnotations.CACHE.createUsage( getSourceModelContext() );
17531734
cacheUsage.region( classDetails.getName() );
1754-
cacheUsage.usage( determineCacheConcurrencyStrategy( context ) );
1735+
cacheUsage.usage( determineCacheConcurrencyStrategy() );
17551736
return cacheUsage;
17561737
}
17571738

1758-
private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(MetadataBuildingContext context) {
1739+
private CacheConcurrencyStrategy determineCacheConcurrencyStrategy() {
17591740
return CacheConcurrencyStrategy.fromAccessType( context.getBuildingOptions().getImplicitCacheAccessType() );
17601741
}
17611742

hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/CacheAnnotation.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ public class CacheAnnotation implements Cache {
1616
private org.hibernate.annotations.CacheConcurrencyStrategy usage;
1717
private String region;
1818
private boolean includeLazy;
19-
private String include;
2019

2120
/**
2221
* Used in creating dynamic annotation instances (e.g. from XML)
2322
*/
2423
public CacheAnnotation(SourceModelBuildingContext modelContext) {
2524
this.region = "";
2625
this.includeLazy = true;
27-
this.include = "all";
2826
}
2927

3028
/**
@@ -34,7 +32,6 @@ public CacheAnnotation(Cache annotation, SourceModelBuildingContext modelContext
3432
this.usage = annotation.usage();
3533
this.region = annotation.region();
3634
this.includeLazy = annotation.includeLazy();
37-
this.include = annotation.include();
3835
}
3936

4037
/**
@@ -44,7 +41,6 @@ public CacheAnnotation(Map<String, Object> attributeValues, SourceModelBuildingC
4441
this.usage = (org.hibernate.annotations.CacheConcurrencyStrategy) attributeValues.get( "usage" );
4542
this.region = (String) attributeValues.get( "region" );
4643
this.includeLazy = (boolean) attributeValues.get( "includeLazy" );
47-
this.include = (String) attributeValues.get( "include" );
4844
}
4945

5046
@Override
@@ -81,15 +77,4 @@ public void includeLazy(boolean value) {
8177
this.includeLazy = value;
8278
}
8379

84-
85-
@Override
86-
public String include() {
87-
return include;
88-
}
89-
90-
public void include(String value) {
91-
this.include = value;
92-
}
93-
94-
9580
}

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedLazyBasicCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void test(SessionFactoryScope scope) {
9494
}
9595

9696
@Cacheable
97-
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all", region = "Person")
97+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = true, region = "Person")
9898
@Entity(name = "Person")
9999
public static class Person {
100100

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void execute(SessionFactoryScope scope) {
133133
@Entity( name = "Document" )
134134
@Table( name = "DOCUMENT" )
135135
@Cacheable
136-
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy", region = "foo" )
136+
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = false, region = "foo" )
137137
static class Document {
138138

139139
@Id

0 commit comments

Comments
 (0)