HHH-16603, HHH-9763 fix some issues with interpreting @Cache/@Cacheable annotations

1. throw instead of logging a WARN if @Cache is on a subclass.
   I just lost 1/2 an hour of my life due to this being a WARN.
   Logging WARNs is a terrible way to report user error!

2. Fix interpretation of SharedCacheMode.UNSPECIFIED
This commit is contained in:
Gavin 2023-05-15 12:42:06 +02:00 committed by Gavin King
parent 43fd8a7ced
commit f709763dab
2 changed files with 19 additions and 16 deletions

View File

@ -1524,22 +1524,22 @@ public class EntityBinder {
}
private void bindSubclassCache(SharedCacheMode sharedCacheMode) {
final Cache cache = annotatedClass.getAnnotation( Cache.class );
if ( annotatedClass.isAnnotationPresent( Cache.class ) ) {
final String className = persistentClass.getClassName() == null
? annotatedClass.getName()
: persistentClass.getClassName();
throw new AnnotationException("Entity class '" + className
+ "' is annotated '@Cache' but it is a subclass in an entity inheritance hierarchy"
+" (only root classes may define second-level caching semantics)");
}
final Cacheable cacheable = annotatedClass.getAnnotation( Cacheable.class );
if ( cache != null ) {
LOG.cacheOrCacheableAnnotationOnNonRoot(
persistentClass.getClassName() == null
? annotatedClass.getName()
: persistentClass.getClassName()
);
}
else if ( cacheable == null && persistentClass.getSuperclass() != null ) {
// we should inherit our super's caching config
isCached = persistentClass.getSuperclass().isCached();
}
else {
isCached = isCacheable( sharedCacheMode, cacheable );
}
isCached = cacheable == null && persistentClass.getSuperclass() != null
// we should inherit the root class caching config
? persistentClass.getSuperclass().isCached()
//TODO: is this even correct?
// Do we even correctly support selectively enabling caching on subclasses like this?
: isCacheable( sharedCacheMode, cacheable );
}
private void bindRootClassCache(SharedCacheMode sharedCacheMode, MetadataBuildingContext context) {
@ -1581,6 +1581,7 @@ public class EntityBinder {
// all entities should be cached
return true;
case ENABLE_SELECTIVE:
case UNSPECIFIED: // Hibernate defaults to ENABLE_SELECTIVE, the only sensible setting
// only entities with @Cacheable(true) should be cached
return explicitCacheableAnn != null && explicitCacheableAnn.value();
case DISABLE_SELECTIVE:

View File

@ -52,7 +52,9 @@ public class QueryResultsCacheImpl implements QueryResultsCache {
final List<?> results,
final SharedSessionContractImplementor session) throws HibernateException {
if ( DEBUG_ENABLED ) {
L2CACHE_LOGGER.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), session.getCacheTransactionSynchronization().getCachingTimestamp() );
L2CACHE_LOGGER.debugf( "Caching query results in region: %s; timestamp=%s",
cacheRegion.getName(),
session.getCacheTransactionSynchronization().getCachingTimestamp() );
}
final CacheItem cacheItem = new CacheItem(