使用SQLQuery q = session.createSQLQuery(query).addEntity("edu", StudentEduPojo.class);
执行SQL本机查询时,JVM会抛出空指针异常。
DBConnectionDao.class
package com.ms.avalon.master.dao.rdbms;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class DBConnectionDao {
protected Session session;
protected DataSource dataSource;
protected SessionFactory sessionFactory;
protected HibernateTemplate hibernateTemplate=null;
protected NamedParameterJdbcTemplate namedJdbcTemplate=null;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
namedJdbcTemplate=new NamedParameterJdbcTemplate(dataSource);
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("session created");
this.sessionFactory = sessionFactory;
session = sessionFactory.getCurrentSession();
hibernateTemplate=new HibernateTemplate(sessionFactory);
}
}
PlacementDaoRdbms.class
package com.ms.avalon.master.dao.rdbms;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import com.ms.avalon.master.beans.PlacementCaseBean;
import com.ms.avalon.master.dao.PlacementDao;
import com.ms.avalon.master.formbeans.PlacementFormBean;
import com.ms.avalon.master.pojos.studentpojos.StudentEduPojo;
import com.ms.avalon.master.pojos.studentpojos.StudentTestPojo;
public class PlacementDaoRdbms extends DBConnectionDao implements PlacementDao {
@Override
public List<StudentEduPojo> searchWithCases(PlacementCaseBean caseBean, PlacementFormBean placementBean) {
String query = "Some complicated SQL query";
SQLQuery q = session.createSQLQuery(query)
.addEntity("edu", StudentEduPojo.class);
List<StudentEduPojo> list = q.list();
return list;
}
}
在PlacementDaoRdbms的会话中使用createSqlQuery函数时,我得到了一个NPE。
以下是堆栈跟踪:
2016年5月11日下午8:52:17 org.apache.catalina.core.StandardWrapperValve调用 严重:Servlet [dispatcher]的Servlet.service()在路径[/Avalon-1.0]的上下文中引发异常[请求处理失败;具有根本原因的嵌套异常是java.lang.NullPointerException] 显示java.lang.NullPointerException 在com.ms.avalon.master.dao.rdbms.PlacementDaoRdbms.searchWithCases(PlacementDaoRdbms.java:108) 在com.ms.avalon.master.business.TalentCaseHandler.searchForCases(TalentCaseHandler.java:61) 在com.ms.avalon.master.service.impl.PlacementServiceImpl.searchForTalent(PlacementServiceImpl.java:26) 在com.ms.avalon.master.controllers.PlacementController.searchForTalent(PlacementController.java:29) 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) 在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119) 在com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316) 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) 在java.lang.Thread.run(未知来源)
另外,这是从父类使用会话的正确方法吗?
感谢您的帮助:)
答案 0 :(得分:0)
PlacementDaoRdbms中的方法searchWithCases是从另一个类调用的(让我们调用A)。我正在使用new关键字创建A对象,导致DI和@Autowired无法正常工作。我知道我应该发布完整的代码,但仍然感谢您的帮助和建议。如果您遇到类似问题,请告诉我们:)