2014-02-27 80 views
1

我有一個GenericDAO類,它是由我所有類型的特定DAO擴展。這個通用的DAO類有下面的方法,它工作正常。通用DAO拋出ClassCastException

protected List<T> findByCriteria(Criterion... criterion) { 
    Criteria crit = getSession().createCriteria(getPersistentClass()); 
    for (Criterion c : criterion) { 
     crit.add(c); 
    } 
    return crit.list(); 
} 

現在,我所做的就是添加另一種更法更多的參數,但它會拋出異常我「java.lang.ClassCastException:Ljava.lang.String;不能轉換爲java.lang.String」 。以下是方法簽名。

public List<T> getByCriteria(int iPage, int iCount, Order order, ProjectionList pl, Criterion... criteria){ 
    Criteria crit = getSession().createCriteria(getPersistentClass()); 
    for (Criterion c : criteria) { 
     crit.add(c); 
    } 
    // set max results to return 
    //crit.setMaxResults(iCount); 
    // set first record in result starting from 
    //crit.setFirstResult((iPage-1)*iCount); 
    //crit.list(); // Doesn't matter even if I just put it one line above without assign 
    return crit.list(); //This is the line which throws exception 
} 

我是想爲增加更多的篩選條件搜索功能,但因爲異常的我註釋掉額外的代碼和我只是模仿工作功能findByCriteria。我在網上搜索,但無法弄清楚如何解決這個問題,或者第一種方法如何正常工作,第二種方法會拋出錯誤。即使由兩者生成的hibernate查詢也是一樣的。

UPDATE: 下面是異常堆棧跟蹤:

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String 
at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:39) 
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:57) 
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) 
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280) 
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275) 
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1969) 
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1940) 
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875) 
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836) 
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816) 
at org.hibernate.loader.Loader.doQuery(Loader.java:900) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342) 
at org.hibernate.loader.Loader.doList(Loader.java:2526) 
at org.hibernate.loader.Loader.doList(Loader.java:2512) 
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342) 
at org.hibernate.loader.Loader.list(Loader.java:2337) 
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124) 
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662) 
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 
at com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl.getByCriteria(GenericHibernateDaoImpl.java:288) 
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 org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
at com.sun.proxy.$Proxy44.getByCriteria(Unknown Source) 
at com.wipro.wcc.services.data.DataLayerWccarchiveImpl.executeCriteria(DataLayerWccarchiveImpl.java:510) 
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 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 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
at com.sun.proxy.$Proxy37.executeCriteria(Unknown Source) 
at com.wipro.wcc.controller.DocumentController.search(DocumentController.java:112) 
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:214) 
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:748) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:724) 

問候,

潘卡

+0

而異常的堆棧跟蹤是... –

回答

1

在我身邊的是一個愚蠢的錯誤。在準備標準數組時,已經傳遞了字符串數組而不是針對like,lt,eq等的值,這是導致該問題的原因。不知道我是如何忽略這一點的。

相關問題