Problems using Hibernate and Spring in web application

Posted by user628480 on Stack Overflow See other posts from Stack Overflow or by user628480
Published on 2011-02-22T14:59:29Z Indexed on 2011/02/22 15:25 UTC
Read the original article Hit count: 290

Filed under:
|
|

Hi.I'm having NullPointerException trying to getCurrentSession()

 java.lang.NullPointerException
 servlets.ControlServlet.doPost(ControlServlet.java:46)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I use Tomcat 5.5 index.jsp page:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%@ page import="java.util.List" %>
<%@ page import="data.Singer" %>
<jsp:useBean id="singer" class="data.Singer" scope="session"/> 
<jsp:setProperty name="singer" property="*" />

<form action="ControlServlet" method="POST">
    <form method=“POST”>
    Name:<br /> 
    <input type=“text” name="name" /><br />
    Type:<br />
    <input type=“text” name="type" /><br />
    <input type="submit" name="Add song" value="Add song">
    <input type="submit" name="save" value="Save" /><br><br>
<input type ="submit" name="values" value="Get values" >
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>webproject</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans.xml, /WEB-INF/conf.xml, /WEB-INF/singers.hbm.xml, /WEB-INF/songs.hbm.xml, /WEB-INF/singerbeans.xml, /WEB-INF/songbeans.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/beans.xml, /WEB-INF/conf.xml, /WEB-INF/singers.hbm.xml, /WEB-INF/songs.hbm.xml, /WEB-INF/singerbeans.xml, /WEB-INF/songbeans.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <description>
    </description>
        <display-name>ControlServlet</display-name>
        <servlet-name>ControlServlet</servlet-name>
        <servlet-class>servlets.ControlServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ControlServlet</servlet-name>
        <url-pattern>/ControlServlet</url-pattern>
    </servlet-mapping>


</web-app>

ControlServlet.java

public class ControlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Autowired
    private SingerDao singerdao; 
    public SingerDao getSingerDao() {
        return singerdao;
    }

    public void setSingerDao(SingerDao singerdao) {
        this.singerdao = singerdao;
    }

    public ControlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("values") != null) {
            response.getWriter().println(singerdao.getDBValues());
        }
    }

}

and SingerDao.java

public class SingerDao implements SingerDaoInterface {
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List getDBValues() {
        Session session = getCurrentSession();
        List<Singer> singers = session.createCriteria(Singer.class).list();
        return singers;
    }

    private org.hibernate.classic.Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public void updateSinger(Singer singer) {
        Session session = getCurrentSession();
        session.update(singer);
    }

    public Singer getSinger(int id) {
        Singer singer = null;
        Session session = getCurrentSession();
        singer = (Singer) session.load(Singer.class, id);
        return singer;

    }

    public void deleteSinger(Singer singer) {
        Session session = getCurrentSession();
        session.delete(singer);
    }

    public void insertRow(Singer singer) {
        Session session = getCurrentSession();
        session.save(singer);
    }



}

In simple Java Project it works fine.I think sessionFactory doesn't autowires,but why? Thanks all.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate