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 c97d538f6c..4c87ae7b8d 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..605e31d1a2 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java @@ -0,0 +1,67 @@ +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.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.stat.Statistics; +import org.junit.Test; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class HibernateSecLvlQueryCache extends AbstractSessionTest { + @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()); + URL cacheURL = Thread.currentThread().getContextClassLoader().getResource("ehCache.xml"); + config.setProperty(Environment.CACHE_PROVIDER_CONFIG, new File(cacheURL.toURI()).getAbsolutePath()); + config.setProperty(Environment.GENERATE_STATISTICS, "true"); + } + + @Test + @Priority(10) + public void initData() { + // Revision 1 + getSession().getTransaction().begin(); + StrTestEntity ste = new StrTestEntity("data"); + getSession().persist(ste); + getSession().getTransaction().commit(); + } + + @Test + public void testSecLvlCacheWithRevisionTypeDiskPersistent() { + /* Invoking the same query twice for caching purpose. */ + invokeSampleCacheableQuery(); + invokeSampleCacheableQuery(); + + assert getQueryCacheStatistics() > 0; + } + + private void invokeSampleCacheableQuery() { + getAuditReader().createQuery().forEntitiesAtRevision(StrTestEntity.class, 1) + .add(new RevisionTypeAuditExpression(RevisionType.ADD, "=")) + .setCacheable(true).getResultList(); + } + + private double getQueryCacheStatistics() { + Statistics stats = getSession().getSessionFactory().getStatistics(); + + double queryCacheHitCount = stats.getQueryCacheHitCount(); + double queryCacheMissCount = stats.getQueryCacheMissCount(); + double queryCacheHitRatio = queryCacheHitCount / (queryCacheHitCount + queryCacheMissCount); + + return queryCacheHitRatio; + } +} diff --git a/hibernate-envers/src/test/resources/ehCache.xml b/hibernate-envers/src/test/resources/ehCache.xml new file mode 100644 index 0000000000..89a746c7be --- /dev/null +++ b/hibernate-envers/src/test/resources/ehCache.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file