【问题标题】:NullPointerException in sessionFactory.openSessionsessionFactory.openSession 中的 NullPointerException
【发布时间】:2013-11-14 06:31:38
【问题描述】:

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

【问题讨论】:

  • 异常状态sessionFactory 引用null,正如它所期望的SessionFactory 对象。检查您的 sessionFactory 对象初始化。
  • 你如何定义sessionFactory
  • 你能把你的 sessionFactory 配置和注入代码分享给 EmployeeDaoImpl 类吗?
  • 您正在构建您自己的 dao 实例(不由 spring 管理),并期望 spring 神奇地注入依赖项。自动装配依赖关系,如果不可能,请使用应用程序上下文来检索 spring 配置的实例。也使用getCurrentSession 而不是openSession 并通过应用正确的声明性事务管理来管理您的会话。

标签: java spring hibernate servlets


【解决方案1】:

当我通过 jsp 文件调用它时,它运行良好。

因为你在你的 DAO 中定义了这个语句,所以它起作用了:

@Autowired
private SessionFactory sessionFactory;

并且调用listEmployees 方法的DAO 实例是Spring 托管bean,并且您在配置文件中定义的Spring 托管SessionFactory 已经注入到您的DAO bean 中。

但是当我通过 servlet 调用它时,它会给出 NullPointerException

这不起作用:

EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees();  

因为调用listEmployeesEmployeeDao 实例不是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>

【讨论】:

  • 感谢您的解释。我明白了错误。当我尝试您的解决方案时,它会在 ApplicationContext context =WebApplicationContextUtils...在 org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84) 在 net.pearson.mail.ConfirmServlet.init(ConfirmServlet.java:38) 在 org.apache.catalina.core.StandardWrapper.initServlet (StandardWrapper.java:1280)...
【解决方案2】:

在第 95 行之前包含以下代码行。假设您尚未创建 SessionFactory 对象

SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
//Your operation
Session.getTransaction().commit();

【讨论】:

  • 我也试过了。当我使用它时,它会停止给出错误。但不传递任何值。
  • 我更新了我的代码,试试这个方法,如果还有错误让我通知一下
【解决方案3】:

对我来说,hibernate.cfg.xml 中的映射属性指向了不正确的模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2017-06-01
    • 2016-11-10
    相关资源
    最近更新 更多