views:

48

answers:

1

Hi,

I am using JPA and Hibernate for the database. I have configured (EHCacache) second level cache and query level cache, but just to make sure that caching is working I was trying to get the statistics which is throwing class cast exception.Any help will be highly appreciated. My main goal is to see all the objects which have been cached to make sure that the caching is working properly.

Here is the code:

public List<CodeValue> findByCodetype(String propertyName) {

    try {
        final String queryString = "select model from CodeValue model where model.codetype"
                + "= :propertyValue" + " order by model.code";

        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        query.setHint("org.hibernate.cacheRegion", "query.findByCodetype");
        query.setParameter("propertyValue", propertyName);
        List resultList = query.getResultList();

        org.hibernate.Session session = (Session) em.getDelegate();
        SessionFactory sessionFactory = session.getSessionFactory();
        Map cacheEntries = sessionFactory.getStatistics()
                .getSecondLevelCacheStatistics("query.findByCodetype")
                .getEntries();
        logger.info("The statistics are: " + cacheEntries);
        return resultList;

    } catch (RuntimeException re) {
        logger.error("findByCodetype failed in trauma patient", re);
        throw re;
    }

}

The error is existing right when I am trying to print the statistics. Below is exception:

[6/7/10 19:23:17:059 GMT] 00000034 SystemOut     O java.lang.ClassCastException: org.hibernate.cache.QueryKey incompatible with org.hibernate.cache.CacheKey
    at org.hibernate.stat.SecondLevelCacheStatistics.getEntries(SecondLevelCacheStatistics.java:51)
    at com.idph.trauma.registry.service.TraumaPatientDAO.findByCodetype(TraumaPatientDAO.java:439)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:615)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy209.findByCodetype(Unknown Source)

Do you know what's going on?

+1  A: 

Hibernate gets stroppy when you use the same L2 cache region for both entities and queries. Normally, it doesn't matter much, it will work fine, but certain operations (like this one) will trigger the bug. The Hibernate developers don't seem inclined to fix it.

Make sure your query cache uses a different cache region to the entity cache.

See forum post for discussion.

skaffman