diff --git a/hibernate-envers/hibernate-envers.gradle b/hibernate-envers/hibernate-envers.gradle index 6f6b87256c..2208f54e86 100644 --- a/hibernate-envers/hibernate-envers.gradle +++ b/hibernate-envers/hibernate-envers.gradle @@ -14,6 +14,9 @@ dependencies { testCompile( libraries.junit ) testCompile( project(':hibernate-testing') ) testCompile( libraries.jpa ) + testCompile( project(':hibernate-ehcache') ) { + exclude group: 'org.slf4j' + } testRuntime( libraries.h2 ) testRuntime( libraries.javassist ) } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/RevisionTypeType.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/RevisionTypeType.java index f473d1264d..98b19a287a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/RevisionTypeType.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/RevisionTypeType.java @@ -38,7 +38,9 @@ import org.hibernate.usertype.UserType; * A hibernate type for the {@link RevisionType} enum. * @author Adam Warski (adam at warski dot org) */ -public class RevisionTypeType implements UserType { +public class RevisionTypeType implements UserType, Serializable { + private static final long serialVersionUID = -1053201518229282688L; + private static final int[] SQL_TYPES = { Types.TINYINT }; public int[] sqlTypes() { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java new file mode 100644 index 0000000000..79677dbdc7 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java @@ -0,0 +1,66 @@ +package org.hibernate.envers.test.integration.cache; + +import org.hibernate.MappingException; +import org.hibernate.cache.internal.EhCacheProvider; +import org.hibernate.cfg.Environment; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.envers.RevisionType; +import org.hibernate.envers.query.criteria.RevisionTypeAuditExpression; +import org.hibernate.envers.test.AbstractSessionTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.testing.TestForIssue; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URISyntaxException; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class HibernateSecLvlQueryCache extends AbstractSessionTest { + private static final String QUERY_CACHE_REGION = "queryCacheRegion"; + + @Override + protected void initMappings() throws MappingException, URISyntaxException { + config.addAnnotatedClass(StrTestEntity.class); + config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true"); + config.setProperty(Environment.USE_QUERY_CACHE, "true"); + config.setProperty(Environment.CACHE_PROVIDER, EhCacheProvider.class.getName()); + config.setProperty(Environment.CACHE_PROVIDER_CONFIG, "ehcache-test.xml"); + } + + @Test + @Priority(10) + public void initData() { + // Revision 1 + getSession().getTransaction().begin(); + StrTestEntity ste = new StrTestEntity("data"); + getSession().persist(ste); + getSession().getTransaction().commit(); + + // Evicting old query cache. + getSession().getSessionFactory().getCache().evictQueryRegion(QUERY_CACHE_REGION); + } + + @Test + @TestForIssue(jiraKey="HHH-5025") + public void testSecLvlCacheWithRevisionTypeDiskPersistent() throws InterruptedException { + // Cached query that requires serializing RevisionType variable when persisting to disk. + getAuditReader().createQuery().forEntitiesAtRevision(StrTestEntity.class, 1) + .add(new RevisionTypeAuditExpression(RevisionType.ADD, "=")) + .setCacheable(true).setCacheRegion(QUERY_CACHE_REGION).getResultList(); + + // Waiting for cached data to persist to disk. + Thread.sleep(1000); + + Assert.assertTrue(getQueryCacheSize() > 0); + } + + private int getQueryCacheSize() { + // Statistics are not notified about persisting cached data failure. However, cache entry gets evicted. + // See DiskWriteTask.call() method (net.sf.ehcache.store.compound.factories.DiskStorageFactory). + SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) getSession().getSessionFactory(); + return sessionFactoryImplementor.getQueryCache(QUERY_CACHE_REGION).getRegion().toMap().size(); + } +} diff --git a/hibernate-envers/src/test/resources/ehcache-test.xml b/hibernate-envers/src/test/resources/ehcache-test.xml new file mode 100644 index 0000000000..d35fb8b048 --- /dev/null +++ b/hibernate-envers/src/test/resources/ehcache-test.xml @@ -0,0 +1,14 @@ + + + + + + + + + \ No newline at end of file