Java sessionFactory.openSession 中的 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19971098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NullPointerException in sessionFactory.openSession
提问by Neha
I'm developing a web application using Spring Hibernate. At there I have a method in DAO class.
我正在使用 Spring Hibernate 开发 Web 应用程序。在那里我在 DAO 类中有一个方法。
When I call it through a jsp file it works well. But When I call it through servlet it gives a NullPointerException
. Below I put the method.
当我通过 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;
}
Below is how I call it.
下面是我如何称呼它。
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
}
This is my sessionFactory configuration
这是我的 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>
And This is the error
这是错误
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)
Could you please tell me how to solve this because I want to call it through servlet.
你能告诉我如何解决这个问题,因为我想通过 servlet 调用它。
采纳答案by Debojit Saikia
When I call it through a jsp file it works well.
当我通过 jsp 文件调用它时,它运行良好。
Its working because you have this statement defined in your DAO:
它的工作原理是因为您在 DAO 中定义了以下语句:
@Autowired
private SessionFactory sessionFactory;
And the DAO instance on which the listEmployees
method was called is a Spring managed bean and the Spring managed SessionFactory
that you have defined in your configuration file has already been injected into your DAO bean.
listEmployees
调用该方法的 DAO 实例是一个 Spring 管理的 bean,并且SessionFactory
您在配置文件中定义的 Spring 管理已经被注入到您的 DAO bean 中。
But When I call it through servlet it gives a NullPointerException
但是当我通过 servlet 调用它时,它给出了一个 NullPointerException
This is not working:
这不起作用:
EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees();
Because the EmployeeDao
instance on which listEmployees
is being called is not a Spring managed bean. Its just a new instance of EmployeeDao
. So the SessionFactory
instance that you have in your EmployeeDao
is null
.
因为被调用的EmployeeDao
实例listEmployees
不是 Spring 托管的 bean。它只是EmployeeDao
. 所以SessionFactory
你的实例EmployeeDao
是null
.
Now, if you want to inject your Spring managed EmployeeDao
into your Servlet, you can do this by overriding the Servlet's init
method as below:
现在,如果您想将 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");
}
You can now re-write getConfirm
method to this:
您现在可以将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
}
EDIT:
编辑:
Make these entries into your web.xml:
将这些条目添加到您的 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>
回答by Kamlesh Arya
Include below line of code before line 95. Assuming that you havent created SessionFactory object
在第 95 行之前包含以下代码行。假设您尚未创建 SessionFactory 对象
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
//Your operation
Session.getTransaction().commit();
回答by Bishal Paudel
For me, the mapping attribute in hibernate.cfg.xml
was pointing to incorrect Model.
对我来说,中的映射属性hibernate.cfg.xml
指向不正确的模型。