我正在使用 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 调用它。