[英]NullPointerException in sessionFactory.openSession
我正在使用Spring Hibernate開發一個Web應用程序。 在那里我有一個DAO類的方法。
當我通過jsp文件調用它時效果很好。 但是當我通過servlet調用它時,它會產生一個NullPointerException
。 下面我把方法。
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
Session session = sessionFactory.openSession();//line 95
Criteria crit= session.createCriteria(Employee.class);
crit.add(Restrictions.eq("EmployeeId",2 ));
List<Employee> employeelist= crit.list();
session.close();
return employeelist;
}
以下是我的稱呼方式。
public void getConfirm(HttpServletRequest request, HttpServletResponse response) {
Logger.getLogger(this.getClass()).warning("Inside Confirm Servlet");
response.setContentType("text/html");
// do some works
EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees(); //line 55
}
這是我的sessionFactory配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>abc.model.Employee</value>
</list>
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
而這是錯誤
SEVERE: Servlet.service() for servlet [ConfirmServlet] in context with path[/Spring3Hibernate] threw exception
java.lang.NullPointerException
at abc.dao.EmployeeDao.listEmployees(EmployeeDao.java:95)
at abc.mail.ConfirmServlet.getConfirm(ConfirmServlet.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
你能告訴我如何解決這個問題,因為我想通過servlet來調用它。
當我通過jsp文件調用它時效果很好。
它的工作原因是你在DAO中定義了這個語句:
@Autowired
private SessionFactory sessionFactory;
調用listEmployees
方法的DAO實例是Spring托管bean,並且已在配置文件中定義的Spring托管的SessionFactory
已經注入到DAO bean中。
但是當我通過servlet調用它時,它會產生一個NullPointerException
這不起作用:
EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees();
因為正在調用listEmployees
的EmployeeDao
實例不是Spring托管bean。 它只是EmployeeDao
一個新實例。 因此, EmployeeDao
的SessionFactory
實例為null
。
現在,如果您想將Spring托管的EmployeeDao
注入到Servlet中,可以通過覆蓋Servlet的init
方法來實現,如下所示:
private EmployeeDao ed;
public void init(ServletConfig config) throws ServletException {
super.init(config);
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
ed = (EmployeeDao) context.getBean("employeeDao");
}
您現在可以重寫getConfirm
方法:
public void getConfirm(HttpServletRequest request, HttpServletResponse response) {
Logger.getLogger(this.getClass()).warning("Inside Confirm Servlet");
response.setContentType("text/html");
// do some works
//EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees(); //line 55
}
編輯:
將這些條目放入web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/your-spring-config-file.xml
</param-value>
</context-param>
在第95行之前包含下面的代碼行。假設你還沒有創建SessionFactory對象
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
//Your operation
Session.getTransaction().commit();
對我來說, hibernate.cfg.xml
的mapping屬性指向不正確的Model。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.