Java 查询级别2缓存引发ClassCastException
我正在为数据库使用JPA和Hibernate。我已经配置了(EHCacache)二级缓存和查询级缓存,但为了确保缓存正常工作,我正在尝试获取引发类强制转换异常的统计信息。任何帮助都将不胜感激。我的主要目标是查看所有已缓存的对象,以确保缓存正常工作 代码如下:Java 查询级别2缓存引发ClassCastException,java,hibernate,caching,jpa,Java,Hibernate,Caching,Jpa,我正在为数据库使用JPA和Hibernate。我已经配置了(EHCacache)二级缓存和查询级缓存,但为了确保缓存正常工作,我正在尝试获取引发类强制转换异常的统计信息。任何帮助都将不胜感激。我的主要目标是查看所有已缓存的对象,以确保缓存正常工作 代码如下: public List<CodeValue> findByCodetype(String propertyName) { try { final String queryString = "select
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;
}
}
您知道发生了什么吗?当您对实体和查询使用相同的二级缓存区域时,Hibernate会变得很麻烦。通常情况下,这并不重要,它可以正常工作,但是某些操作(比如这个)会触发bug。Hibernate开发人员似乎不想修复它 确保您的应用程序已访问实体缓存 请参阅以进行讨论
[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)