1111import java .util .HashSet ;
1212import java .util .Iterator ;
1313import java .util .List ;
14- import java .util .Locale ;
1514import java .util .Map ;
1615import java .util .Set ;
1716
127126import jakarta .persistence .PrimaryKeyJoinColumns ;
128127import jakarta .persistence .SecondaryTable ;
129128import jakarta .persistence .SecondaryTables ;
130- import jakarta .persistence .SharedCacheMode ;
131129import jakarta .persistence .UniqueConstraint ;
132130
133131import 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
0 commit comments