hibernate获取记录行数异常
使用hibernate时来查询符合条件的记录条数时出现如下的异常:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integerat org.hibernate.type.IntegerType.set(IntegerType.java:64)at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:154)at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1732)at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1703)at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1593)at org.hibernate.loader.Loader.doQuery(Loader.java:696)at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)at org.hibernate.loader.Loader.doList(Loader.java:2232)at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)at org.hibernate.loader.Loader.list(Loader.java:2124)at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:328)at org.edu.scut.lab24.uam.service.impl.PostsServiceImpl.getAllEntityCount(PostsServiceImpl.java:687)at org.edu.scut.lab24.uam.service.impl.PostsServiceImpl$$FastClassByCGLIB$$e0aeb7ee.invoke(<generated>)at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)at org.edu.scut.lab24.interceptor.CacheInterceptor.beforeGet(CacheInterceptor.java:55)at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:622)at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:611)at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)at org.edu.scut.lab24.uam.service.impl.PostsServiceImpl$$EnhancerByCGLIB$$1b7fcf7b.getAllEntityCount(<generated>)at org.edu.scut.lab24.uam.service.impl.PostsServiceImpl$$FastClassByCGLIB$$e0aeb7ee.invoke(<generated>)at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)at org.edu.scut.lab24.uam.service.impl.PostsServiceImpl$$EnhancerByCGLIB$$f89aba2f.getAllEntityCount(<generated>)at org.edu.scut.lab24.web.controller.posts.PostsManagerController.getsearPosts(PostsManagerController.java:451)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)at java.lang.Thread.run(Thread.java:619)
?
获取记录数的部分代码如下:
?
Criteria c = baseDao.getCriteria(Post.class);c.createAlias("obj","obj");c.add(Restrictions.eq("obj.objectPK", Long.parseLong(objPK)));c.add(Restrictions.ne("title", "回复"));c.add(Restrictions.ne("recommended", "3"));Object object = c.setProjection(Projections.rowCount()).uniqueResult();count = ((Integer)object).intValue();
出现这个异常的主要原因是在
c.add(Restrictions.ne("recommended", "3"));
?这句代码上,因为recommended为int型,所以这里使用string类型显示不对,所以可以修改为:
c.add(Restrictions.ne("recommended", new Integer(3)));
?由于java自动装拆包技术,所以可以直接写成:
c.add(Restrictions.ne("recommended",3));?