From db885b85bb5665f3ccfa6458fb277fe1c31508b3 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 15 Mar 2017 23:09:56 -0700 Subject: [PATCH] HHH-11143 : Log a warning if @Cache / @Cacheable specified on non-root entities --- .../cfg/annotations/EntityBinder.java | 5 ++ .../hibernate/internal/CoreMessageLogger.java | 4 + .../NonRootEntityWithCacheAnnotationTest.java | 85 +++++++++++++++++++ ...RootEntityWithCacheableAnnotationTest.java | 85 +++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheAnnotationTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheableAnnotationTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 1a7d84c465..fc300cc712 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -12,6 +12,7 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import javax.persistence.Access; +import javax.persistence.Cacheable; import javax.persistence.ConstraintMode; import javax.persistence.Entity; import javax.persistence.InheritanceType; @@ -302,6 +303,10 @@ public class EntityBinder { if (annotatedClass.isAnnotationPresent(Immutable.class)) { LOG.immutableAnnotationOnNonRoot(annotatedClass.getName()); } + if ( annotatedClass.isAnnotationPresent( Cacheable.class ) || + annotatedClass.isAnnotationPresent( Cache.class ) ) { + LOG.cacheOrCacheableAnnotationOnNonRoot( annotatedClass.getName() ); + } } persistentClass.setOptimisticLockStyle( getVersioning( optimisticLockType ) ); persistentClass.setSelectBeforeUpdate( selectBeforeUpdate ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 3a0e891674..4672ab4865 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -1766,4 +1766,8 @@ public interface CoreMessageLogger extends BasicLogger { "implementing equals/hashCode." ) void unknownJavaTypeNoEqualsHashCode(Class javaType); + + @LogMessage(level = WARN) + @Message(value = "@javax.persistence.Cacheable or @org.hibernate.annotations.Cache used on a non-root entity: ignored for %s", id = 482) + void cacheOrCacheableAnnotationOnNonRoot(String className); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheAnnotationTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheAnnotationTest.java new file mode 100644 index 0000000000..ce48cdda80 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheAnnotationTest.java @@ -0,0 +1,85 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.cache; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.SharedCacheMode; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.cfg.annotations.EntityBinder; +import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.service.spi.ServiceRegistryImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.cache.CachingRegionFactory; +import org.hibernate.testing.logger.LoggerInspectionRule; +import org.hibernate.testing.logger.Triggerable; +import org.junit.Rule; +import org.junit.Test; + +import org.jboss.logging.Logger; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Gail Badner + */ +@TestForIssue( jiraKey = "HHH-11143") +public class NonRootEntityWithCacheAnnotationTest { + + @Rule + public LoggerInspectionRule logInspection = new LoggerInspectionRule( + Logger.getMessageLogger( CoreMessageLogger.class, EntityBinder.class.getName() ) + ); + + @Test + public void testCacheOnNonRootEntity() { + Map settings = new HashMap(); + settings.put( Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); + settings.put( AvailableSettings.JPA_SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE ); + + ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() + .applySettings( settings ) + .build(); + + Triggerable triggerable = logInspection.watchForLogMessages( "HHH000482" ); + + Metadata metadata = new MetadataSources( serviceRegistry ) + .addAnnotatedClass( ABase.class ) + .addAnnotatedClass( AEntity.class ) + .buildMetadata(); + + assertTrue( triggerable.wasTriggered() ); + assertNull( ( metadata.getEntityBinding( AEntity.class.getName() ) ).getCacheConcurrencyStrategy() ); + + serviceRegistry.destroy(); + } + + @Entity + @Inheritance + public static class ABase { + @Id + private Long id; + } + + @Entity + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) + public static class AEntity extends ABase { + private String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheableAnnotationTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheableAnnotationTest.java new file mode 100644 index 0000000000..7a0ed58916 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/NonRootEntityWithCacheableAnnotationTest.java @@ -0,0 +1,85 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.cache; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.Cacheable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.SharedCacheMode; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.cfg.annotations.EntityBinder; +import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.service.spi.ServiceRegistryImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.cache.CachingRegionFactory; +import org.hibernate.testing.logger.LoggerInspectionRule; +import org.hibernate.testing.logger.Triggerable; +import org.junit.Rule; +import org.junit.Test; + +import org.jboss.logging.Logger; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Gail Badner + */ +@TestForIssue( jiraKey = "HHH-11143") +public class NonRootEntityWithCacheableAnnotationTest { + + @Rule + public LoggerInspectionRule logInspection = new LoggerInspectionRule( + Logger.getMessageLogger( CoreMessageLogger.class, EntityBinder.class.getName() ) + ); + + @Test + public void testCacheableOnNonRootEntity() { + Map settings = new HashMap(); + settings.put( Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); + settings.put( AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "read-write" ); + settings.put( AvailableSettings.JPA_SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE ); + + ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() + .applySettings( settings ) + .build(); + + Triggerable triggerable = logInspection.watchForLogMessages( "HHH000482" ); + + Metadata metadata = new MetadataSources( serviceRegistry ) + .addAnnotatedClass( ABase.class ) + .addAnnotatedClass( AEntity.class ) + .buildMetadata(); + + assertTrue( triggerable.wasTriggered() ); + assertNull( ( metadata.getEntityBinding( AEntity.class.getName() ) ).getCacheConcurrencyStrategy() ); + + serviceRegistry.destroy(); + } + + @Entity + @Inheritance + public static class ABase { + @Id + private Long id; + } + + @Entity + @Cacheable + public static class AEntity extends ABase { + private String name; + } +}