我正在为数据库使用JPA和Hibernate。我已经配置了(EHCacache)二级缓存和查询级缓存,但只是为了确保缓存正常工作我试图获取抛出类抛出异常的统计信息。任何帮助都将受到高度赞赏。我的主要目标是查看所有已缓存的对象,以确保缓存正常工作。
以下是代码:
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;
}
}
当我尝试打印统计信息时,错误存在。 以下是例外:
[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)
你知道发生了什么吗?
答案 0 :(得分:1)
当您对实体和查询使用相同的L2缓存区域时,Hibernate会变得非常粗糙。通常情况下,它并不重要,它会正常工作,但某些操作(如此操作)将触发错误。 Hibernate开发人员似乎并不倾向于修复它。
确保query cache uses a different cache region到实体缓存。
请参阅forum post进行讨论。