Search in sources :

Example 1 with NaturalIdCache

use of org.hibernate.annotations.NaturalIdCache in project hibernate-orm by hibernate.

the class EntityBinder method applyCaching.

public void applyCaching(XClass clazzToProcess, SharedCacheMode sharedCacheMode, MetadataBuildingContext context) {
    final Cache explicitCacheAnn = clazzToProcess.getAnnotation(Cache.class);
    final Cacheable explicitCacheableAnn = clazzToProcess.getAnnotation(Cacheable.class);
    isCached = false;
    cacheConcurrentStrategy = null;
    cacheRegion = null;
    cacheLazyProperty = true;
    if (persistentClass instanceof RootClass) {
        Cache effectiveCacheAnn = explicitCacheAnn;
        if (explicitCacheAnn != null) {
            // preserve legacy behavior of circumventing SharedCacheMode when Hibernate's @Cache is used.
            isCached = true;
        } else {
            effectiveCacheAnn = buildCacheMock(clazzToProcess.getName(), context);
            switch(sharedCacheMode) {
                case ALL:
                    {
                        // all entities should be cached
                        isCached = true;
                        break;
                    }
                case ENABLE_SELECTIVE:
                    {
                        if (explicitCacheableAnn != null && explicitCacheableAnn.value()) {
                            isCached = true;
                        }
                        break;
                    }
                case DISABLE_SELECTIVE:
                    {
                        if (explicitCacheableAnn == null || explicitCacheableAnn.value()) {
                            isCached = true;
                        }
                        break;
                    }
                default:
                    {
                        // treat both NONE and UNSPECIFIED the same
                        isCached = false;
                        break;
                    }
            }
        }
        cacheConcurrentStrategy = resolveCacheConcurrencyStrategy(effectiveCacheAnn.usage());
        cacheRegion = effectiveCacheAnn.region();
        switch(effectiveCacheAnn.include().toLowerCase(Locale.ROOT)) {
            case "all":
                {
                    cacheLazyProperty = true;
                    break;
                }
            case "non-lazy":
                {
                    cacheLazyProperty = false;
                    break;
                }
            default:
                {
                    throw new AnnotationException("Unknown @Cache.include value [" + effectiveCacheAnn.include() + "] : " + annotatedClass.getName());
                }
        }
    } else {
        if (explicitCacheAnn != null) {
            LOG.cacheOrCacheableAnnotationOnNonRoot(persistentClass.getClassName());
        } else if (explicitCacheableAnn == null && persistentClass.getSuperclass() != null) {
            // we should inherit our super's caching config
            isCached = persistentClass.getSuperclass().isCached();
        } else {
            switch(sharedCacheMode) {
                case ALL:
                    {
                        // all entities should be cached
                        isCached = true;
                        break;
                    }
                case ENABLE_SELECTIVE:
                    {
                        // only entities with @Cacheable(true) should be cached
                        if (explicitCacheableAnn != null && explicitCacheableAnn.value()) {
                            isCached = true;
                        }
                        break;
                    }
                case DISABLE_SELECTIVE:
                    {
                        if (explicitCacheableAnn == null || !explicitCacheableAnn.value()) {
                            isCached = true;
                        }
                        break;
                    }
                default:
                    {
                        // treat both NONE and UNSPECIFIED the same
                        isCached = false;
                        break;
                    }
            }
        }
    }
    naturalIdCacheRegion = null;
    final NaturalIdCache naturalIdCacheAnn = clazzToProcess.getAnnotation(NaturalIdCache.class);
    if (naturalIdCacheAnn != null) {
        if (BinderHelper.isEmptyAnnotationValue(naturalIdCacheAnn.region())) {
            if (explicitCacheAnn != null && StringHelper.isNotEmpty(explicitCacheAnn.region())) {
                naturalIdCacheRegion = explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX;
            } else {
                naturalIdCacheRegion = clazzToProcess.getName() + NATURAL_ID_CACHE_SUFFIX;
            }
        } else {
            naturalIdCacheRegion = naturalIdCacheAnn.region();
        }
    }
}
Also used : RootClass(org.hibernate.mapping.RootClass) Cacheable(javax.persistence.Cacheable) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) AnnotationException(org.hibernate.AnnotationException) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) Cache(org.hibernate.annotations.Cache)

Aggregations

Cacheable (javax.persistence.Cacheable)1 AnnotationException (org.hibernate.AnnotationException)1 Cache (org.hibernate.annotations.Cache)1 NaturalIdCache (org.hibernate.annotations.NaturalIdCache)1 RootClass (org.hibernate.mapping.RootClass)1