remove deprecated and very ugly member of @Cache

This commit is contained in:
Gavin King 2024-12-01 14:49:30 +01:00
parent 05852b706f
commit 2a4f4304c7
5 changed files with 39 additions and 87 deletions

View File

@ -89,18 +89,4 @@ public @interface Cache {
* @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:
* <ul>
* <li>{@code "all"} properties, the default, or
* <li>only {@code "non-lazy"} properties.
* </ul>
*
* @deprecated Use {@link #includeLazy()} for the sake of typesafety.
*/
@Deprecated(since="6.4")
String include() default "all";
}

View File

@ -11,7 +11,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@ -127,7 +126,6 @@ import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.SecondaryTables;
import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.UniqueConstraint;
import static jakarta.persistence.InheritanceType.SINGLE_TABLE;
@ -1639,24 +1637,24 @@ public class EntityBinder {
}
private void bindNaturalIdCache() {
naturalIdCacheRegion = null;
final NaturalIdCache naturalIdCacheAnn =
annotatedClass.getAnnotationUsage( NaturalIdCache.class, getSourceModelContext() );
if ( naturalIdCacheAnn == null ) {
return;
}
final String region = naturalIdCacheAnn.region();
if ( region.isBlank() ) {
final Cache explicitCacheAnn = annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
naturalIdCacheRegion =
explicitCacheAnn != null && isNotBlank( explicitCacheAnn.region() )
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
if ( naturalIdCacheAnn != null ) {
final String region = naturalIdCacheAnn.region();
if ( region.isBlank() ) {
final Cache explicitCacheAnn =
annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
naturalIdCacheRegion =
explicitCacheAnn != null && isNotBlank( explicitCacheAnn.region() )
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
}
else {
naturalIdCacheRegion = naturalIdCacheAnn.region();
}
}
else {
naturalIdCacheRegion = naturalIdCacheAnn.region();
naturalIdCacheRegion = null;
}
}
@ -1666,16 +1664,15 @@ public class EntityBinder {
cacheRegion = null;
cacheLazyProperty = true;
queryCacheLayout = null;
final SharedCacheMode sharedCacheMode = context.getBuildingOptions().getSharedCacheMode();
if ( persistentClass instanceof RootClass ) {
bindRootClassCache( sharedCacheMode, context );
if ( isRootEntity() ) {
bindRootClassCache();
}
else {
bindSubclassCache( sharedCacheMode );
bindSubclassCache();
}
}
private void bindSubclassCache(SharedCacheMode sharedCacheMode) {
private void bindSubclassCache() {
if ( annotatedClass.hasAnnotationUsage( Cache.class, getSourceModelContext() ) ) {
final String className = persistentClass.getClassName() == null
? annotatedClass.getName()
@ -1691,46 +1688,30 @@ public class EntityBinder {
? persistentClass.getSuperclass().isCached()
//TODO: is this even correct?
// Do we even correctly support selectively enabling caching on subclasses like this?
: isCacheable( sharedCacheMode, cacheable );
: isCacheable( cacheable );
}
private void bindRootClassCache(SharedCacheMode sharedCacheMode, MetadataBuildingContext context) {
final Cache cache = annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
final Cacheable cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, getSourceModelContext() );
final Cache effectiveCache;
if ( cache != null ) {
// preserve legacy behavior of circumventing SharedCacheMode when Hibernate's @Cache is used.
isCached = true;
effectiveCache = cache;
}
else {
effectiveCache = buildCacheMock( annotatedClass, context );
isCached = isCacheable( sharedCacheMode, cacheable );
}
private void bindRootClassCache() {
final SourceModelBuildingContext sourceModelContext = getSourceModelContext();
final Cache cache = annotatedClass.getAnnotationUsage( Cache.class, sourceModelContext );
final Cacheable cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, sourceModelContext );
// preserve legacy behavior of circumventing SharedCacheMode when Hibernate @Cache is used
final Cache effectiveCache = cache != null ? cache : buildCacheMock( annotatedClass );
isCached = cache != null || isCacheable( cacheable );
cacheConcurrentStrategy = getCacheConcurrencyStrategy( effectiveCache.usage() );
cacheRegion = effectiveCache.region();
cacheLazyProperty = isCacheLazy( effectiveCache, annotatedClass );
cacheLazyProperty = effectiveCache.includeLazy();
final QueryCacheLayout queryCache =
annotatedClass.getAnnotationUsage( QueryCacheLayout.class, getSourceModelContext() );
annotatedClass.getAnnotationUsage( QueryCacheLayout.class, sourceModelContext );
queryCacheLayout = queryCache == null ? null : queryCache.layout();
}
private static boolean isCacheLazy(Cache effectiveCache, ClassDetails annotatedClass) {
if ( !effectiveCache.includeLazy() ) {
return false;
}
return switch ( effectiveCache.include().toLowerCase( Locale.ROOT ) ) {
case "all" -> true;
case "non-lazy" -> false;
default -> throw new AnnotationException(
"Class '" + annotatedClass.getName()
+ "' has a '@Cache' with undefined option 'include=\"" + effectiveCache.include() + "\"'" );
};
}
private static boolean isCacheable(SharedCacheMode sharedCacheMode, Cacheable explicitCacheableAnn) {
return switch ( sharedCacheMode ) {
private boolean isCacheable(Cacheable explicitCacheableAnn) {
return switch ( context.getBuildingOptions().getSharedCacheMode() ) {
case ALL ->
// all entities should be cached
true;
@ -1747,15 +1728,15 @@ public class EntityBinder {
};
}
private static Cache buildCacheMock(ClassDetails classDetails, MetadataBuildingContext context) {
private Cache buildCacheMock(ClassDetails classDetails) {
final CacheAnnotation cacheUsage =
HibernateAnnotations.CACHE.createUsage( context.getMetadataCollector().getSourceModelBuildingContext() );
HibernateAnnotations.CACHE.createUsage( getSourceModelContext() );
cacheUsage.region( classDetails.getName() );
cacheUsage.usage( determineCacheConcurrencyStrategy( context ) );
cacheUsage.usage( determineCacheConcurrencyStrategy() );
return cacheUsage;
}
private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(MetadataBuildingContext context) {
private CacheConcurrencyStrategy determineCacheConcurrencyStrategy() {
return CacheConcurrencyStrategy.fromAccessType( context.getBuildingOptions().getImplicitCacheAccessType() );
}

View File

@ -16,7 +16,6 @@ public class CacheAnnotation implements Cache {
private org.hibernate.annotations.CacheConcurrencyStrategy usage;
private String region;
private boolean includeLazy;
private String include;
/**
* Used in creating dynamic annotation instances (e.g. from XML)
@ -24,7 +23,6 @@ public class CacheAnnotation implements Cache {
public CacheAnnotation(SourceModelBuildingContext modelContext) {
this.region = "";
this.includeLazy = true;
this.include = "all";
}
/**
@ -34,7 +32,6 @@ public class CacheAnnotation implements Cache {
this.usage = annotation.usage();
this.region = annotation.region();
this.includeLazy = annotation.includeLazy();
this.include = annotation.include();
}
/**
@ -44,7 +41,6 @@ public class CacheAnnotation implements Cache {
this.usage = (org.hibernate.annotations.CacheConcurrencyStrategy) attributeValues.get( "usage" );
this.region = (String) attributeValues.get( "region" );
this.includeLazy = (boolean) attributeValues.get( "includeLazy" );
this.include = (String) attributeValues.get( "include" );
}
@Override
@ -81,15 +77,4 @@ public class CacheAnnotation implements Cache {
this.includeLazy = value;
}
@Override
public String include() {
return include;
}
public void include(String value) {
this.include = value;
}
}

View File

@ -94,7 +94,7 @@ public class UninitializedLazyBasicCacheTest {
}
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all", region = "Person")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = true, region = "Person")
@Entity(name = "Person")
public static class Person {

View File

@ -133,7 +133,7 @@ public class InitFromCacheTest {
@Entity( name = "Document" )
@Table( name = "DOCUMENT" )
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy", region = "foo" )
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, includeLazy = false, region = "foo" )
static class Document {
@Id