spring3+hibernate4+ehcache,使用原生sql查询后得到的缓存,从缓存查询数据报错

时间:2021-12-31 09:25:52
如题,我两次使用的sql语句是同一条,第一次从数据库查询,第二次从缓存查询。
dao代码如下:
Query query = getCurrentSession().createSQLQuery(sql).addScalar("name")
  .setCacheable(true);
return query.list();

controller调用代码:
baseService.findBySql("select user.name as name from SYS_USER user");
报错信息如下:
org.springframework.web.method.HandlerMethodjava.lang.NullPointerException
at org.hibernate.cache.internal.StandardQueryCache.get(StandardQueryCache.java:193)
at org.hibernate.loader.Loader.getResultFromQueryCache(Loader.java:2480)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2388)
at org.hibernate.loader.Loader.list(Loader.java:2361)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at com.zoe.rbac.dao.base.impl.BaseDaoImpl.findBySql(BaseDaoImpl.java:173)
at com.zoe.rbac.dao.base.impl.BaseDaoImpl$$FastClassBySpringCGLIB$$8676b2c3.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at net.bull.javamelody.MonitoringSpringInterceptor.invoke(MonitoringSpringInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.zoe.rbac.dao.base.impl.BaseDaoImpl$$EnhancerBySpringCGLIB$$cace9eae.findBySql(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at net.bull.javamelody.MonitoringSpringInterceptor.invoke(MonitoringSpringInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy37.findBySql(Unknown Source)
at com.zoe.rbac.service.base.impl.BaseServiceImpl.findBySql(BaseServiceImpl.java:155)
at com.zoe.rbac.service.base.impl.BaseServiceImpl$$FastClassBySpringCGLIB$$459ad9c9.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at net.bull.javamelody.MonitoringSpringInterceptor.invoke(MonitoringSpringInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.zoe.rbac.service.base.impl.BaseServiceImpl$$EnhancerBySpringCGLIB$$4c4a03e3.findBySql(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at net.bull.javamelody.MonitoringSpringInterceptor.invoke(MonitoringSpringInterceptor.java:73)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy39.findBySql(Unknown Source)
at com.zoe.rbac.controller.UserController.index(UserController.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at .....
可以肯定的是缓存肯定有数据。
如果我用原生sql查询所有字段的话,就不会报错,用hql查询某个字段也不会有问题。
请问这是什么问题?

3 个解决方案

#1


去问 百度 好一点  这里 不太容易得到结果

#2


找到原因了,getCurrentSession().createSQLQuery(sql)..addScalar("name"),要改成...addScalar("name", StringType.INSTANCE),就是后面要加上类型,而且我用的是hibernate4,所以不能用Hibernate.STRING。具体的类型可以参考链接:http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/type/Type.html

#3


我之前写了个mybatis+ehcache的 你可以看看                  点击  spring3+hibernate4+ehcache,使用原生sql查询后得到的缓存,从缓存查询数据报错

#1


去问 百度 好一点  这里 不太容易得到结果

#2


找到原因了,getCurrentSession().createSQLQuery(sql)..addScalar("name"),要改成...addScalar("name", StringType.INSTANCE),就是后面要加上类型,而且我用的是hibernate4,所以不能用Hibernate.STRING。具体的类型可以参考链接:http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/type/Type.html

#3


我之前写了个mybatis+ehcache的 你可以看看                  点击  spring3+hibernate4+ehcache,使用原生sql查询后得到的缓存,从缓存查询数据报错