2010-06-07 2 views
2

J'utilise JPA et Hibernate pour la base de données. J'ai configuré (EHCacache) le cache de second niveau et le cache de niveau de requête, mais juste pour m'assurer que la mise en cache fonctionne, j'essayais d'obtenir les statistiques qui lançaient l'exception de cast de classe. Toute aide sera très appréciée. Mon objectif principal est de voir tous les objets qui ont été mis en cache pour s'assurer que la mise en cache fonctionne correctement.Niveau 2 Lancement de la mise en cache ClassCastException

Voici le 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; 
    } 

} 

L'erreur est droit existant quand je suis en train d'imprimer les statistiques. est ci-dessous 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) 

Savez-vous ce qui se passe?

Répondre

1

Hibernate devient tronqué lorsque vous utilisez la même région de cache L2 pour les entités et les requêtes. Normalement, peu importe, cela fonctionnera bien, mais certaines opérations (comme celle-ci) vont déclencher le bug. Les développeurs d'Hibernate ne semblent pas enclins à le réparer.

Assurez-vous que votre query cache uses a different cache region au cache d'entité. Pour plus d'informations, voir forum post.

Questions connexes