HHH-11143 : Log a warning if @Cache / @Cacheable specified on non-root entities

This commit is contained in:
Gail Badner 2017-03-15 23:09:56 -07:00
parent 31fbdbb0f0
commit db885b85bb
4 changed files with 179 additions and 0 deletions

View File

@ -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 );

View File

@ -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);
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}