从数据库中获取所有记录时,我遇到了此异常,但是我无法弄清楚到底是什么问题,这是我的AbstractDao类的样子
package com.javarnd.pns.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractDao<PK extends Serializable, T> {
private final Class<T> persistentClass;
@SuppressWarnings("unchecked")
public AbstractDao() {
this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
.getActualTypeArguments()[1];
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
// return entityManager.getReference(persistentClass, key);
}
public void persist(T entity) {
getSession().persist(entity);
// entityManager.persist(entity);
}
public void delete(T entity) {
getSession().delete(entity);
// entityManager.remove(entity);
}
protected Criteria createEntityCriteria() {
return (Criteria) getSession().getCriteriaBuilder().createQuery(persistentClass);
// return (Criteria) entityManager.getCriteriaBuilder().createQuery(persistentClass);
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return (Session) sessionFactory.getCurrentSession();
}
// @PersistenceContext
// private EntityManager entityManager;
}
控制器摘要
@GetMapping("/consignee")
public ModelAndView showRequestConsignmentForm(HttpServletRequest request) {
ModelAndView mav = new ModelAndView("consignee/cas-request_service");
List<CompanyDetails> compList = null;
try {
compList = compService.findAll(); // Arise Here
} catch (Exception e) {
e.printStackTrace();
}
mav.addObject("isConsignee", true);
mav.addObject("cList", compList);
return mav;
}
DAOImpl片段(通过Service类调用)
@Override
public List<CompanyDetails> findAll() {
Criteria criteria = createEntityCriteria();
return (List<CompanyDetails>) criteria.list();
}
完成跟踪
java.lang.ClassCastException: org.hibernate.query.criteria.internal.CriteriaQueryImpl cannot be cast to org.hibernate.Criteria
at com.javarnd.pns.dao.AbstractDao.createEntityCriteria(AbstractDao.java:38)
at com.javarnd.pns.dao.CompanyDetailDaoImpl.findAll(CompanyDetailDaoImpl.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy199.findAll(Unknown Source)
at com.javarnd.pns.service.CompanyDetailService.findAll(CompanyDetailService.java:18)
at com.javarnd.pns.controller.ConsigneeController.showRequestConsignmentForm(ConsigneeController.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我需要你们的帮助,以便我能继续进行下去,也可以为我建议最佳的解决方案。
答案 0 :(得分:0)
我无法完全检查代码,因为缺少导入。但是
return (Criteria) getSession().getCriteriaBuilder().createQuery(persistentClass);
对我来说可疑。 createQuery
返回Criteria
吗?也许这是一个查询。
答案 1 :(得分:0)
CriteriaBuilder.createQuery返回CriteriaQuery
,而不返回Criteria
对象。
按如下所示编辑createEntityCriteria
方法:
protected CriteriaQuery createEntityCriteria() {
return (CriteriaQuery) getSession().getCriteriaBuilder().createQuery(persistentClass);
}
和您的findAll
方法:
@Override
public List<CompanyDetails> findAll() {
TypedQuery<CompanyDetails> q = createEntityCriteria();
return q.getResultList();
}