title: Convert Spring LocalSessionFactoryBean to Hibernate SessionFactory
body: <p>I am trying to Integrate Spring with Hibernate. However, I am not able to get Hibernate's SessionFactory Object through Spring's LocalSessionFactoryBean.</p>
  
  <p>I tried the following approaches:</p>
  
  <p>1) Use either of org.springframework.orm.hibernate3 and org.springframework.orm.hibernate4 LocalSessionFactoryBean class</p>
  
  <p>2) Use AbstractSessionFactoryBean class</p>
  
  <p>3)  Try with SessionFactory=LocalSessionFactoryBean.getObject() as well as SessionFactory=LocalSessionFactoryBean</p>
  
  <p>Here's the Project Structure: </p>
  
  <pre><code>Not allowed to post images till I reach 10 credits, sad..
  </code></pre>
  
  <p>Here 's the BookService</p>
  
  <pre><code>    package com.zzz.service;
  
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.orm.hibernate3.AbstractSessionFactoryBean;
      import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  
      import com.zzz.forms.BookForm;
  
      public class BookService {
  
         @Autowired
         LocalSessionFactoryBean hibernateSessionFactory;
  
         SessionFactory sessionFactory;
  
          public LocalSessionFactoryBean getHibernateSessionFactory() {
              return hibernateSessionFactory;
          }
          public void setHibernateSessionFactory(LocalSessionFactoryBean hibernateSessionFactory) {
              this.hibernateSessionFactory = hibernateSessionFactory;
          }
  
  
  
  
  
          public SessionFactory getSessionFactory() {
              return sessionFactory;
          }
          public void setSessionFactory(SessionFactory sessionFactory) {
              this.sessionFactory = sessionFactory;
          }
          public  void  storeBookDetails(BookForm bookForm){
  
              System.out.println("Hibernae");
              setSessionFactory((SessionFactory)hibernateSessionFactory.getObject());
  
              Session session=sessionFactory.openSession();
  
              session.beginTransaction();
              session.save(bookForm);
              session.getTransaction().commit();
              session.close();
  
  
  
              System.out.println("Hibernae");
          }
  
  
  
  
      }
  </code></pre>
  
  <p>Here's The Controller that leads to this service</p>
  
  <pre><code>    package com.zzz.controllers;
  
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;
  
      import com.zzz.forms.BookForm;
      import com.zzz.service.BookService;
  
      @Controller
      public class FirstPageController {
  
  
          BookService bookService;
  
  
          public BookService getBookService() {
              return  new BookService();
          }
  
          public void setBookService(BookService bookService) {
              this.bookService = bookService;
          }
  
          @RequestMapping(value="/firstPage")
          public ModelAndView showFirstPage()
          {
  
              return new ModelAndView("books/Book","BookForm",new BookForm());
  
          }
  
          @RequestMapping(value="/enterBookDetails")
          public ModelAndView enterBookDetails(@ModelAttribute("BookForm") BookForm bookForm)
          {
  
              getBookService().storeBookDetails(bookForm);
  
              System.out.println("BOOK DETAILS ARE AS FOLLOWWS");
              System.out.println(bookForm.getBookId());
              System.out.println(bookForm.getBookName());
  
  
              return  new ModelAndView("books/BookSubmitted","BookForm",bookForm);
  
          }
  
  
      }
  </code></pre>
  
  <p>Application Context: </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:mvc="http://www.springframework.org/schema/mvc"
              xmlns:hz="http://www.hazelcast.com/schema/spring"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.hazelcast.com/schema/spring
              http://www.hazelcast.com/schema/spring/hazelcast-spring-3.2.xsd"&gt;
  
  
              &lt;context:annotation-config/&gt;
              &lt;context:component-scan base-package="com.zzz.controllers"&gt;&lt;/context:component-scan&gt;
              &lt;mvc:annotation-driven/&gt;
  
  
  
              &lt;bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
                  &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
                  &lt;property name="url" value="jdbc:mysql://localhost/zz" /&gt;
                  &lt;property name="username" value="root" /&gt;
                  &lt;property name="password" value="root" /&gt;
              &lt;/bean&gt;
  
              &lt;bean id="hibernateSessionFactory"
                  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" &gt;
                  &lt;property name="dataSource" ref="myDataSource" /&gt;
                  &lt;property name="packagesToScan" value="com.zzz.forms"/&gt;
  
                  &lt;property name="hibernateProperties"&gt;
                      &lt;value&gt;
                          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                      &lt;/value&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
              &lt;/beans&gt;
  </code></pre>
  
  <p>And The web XML</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
            &lt;display-name&gt;ZZZ&lt;/display-name&gt;
  
  
            &lt;welcome-file-list&gt;
              &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
            &lt;/welcome-file-list&gt;
  
  
             &lt;servlet&gt;
              &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
              &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
              &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
            &lt;/servlet&gt;
  
            &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
              &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
            &lt;/servlet-mapping&gt;
  
              &lt;listener&gt;
                  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
              &lt;/listener&gt;
              &lt;context-param&gt;
                  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
                  &lt;param-value&gt;classpath:ApplicationContext.xml&lt;/param-value&gt;
              &lt;/context-param&gt;
  
          &lt;/web-app&gt;
  </code></pre>
  
  <p>The NullPointer Looks like this</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
  org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  
  root cause
  
  java.lang.NullPointerException
  com.zzz.service.BookService.storeBookDetails(BookService.java:38)
  com.zzz.controllers.FirstPageController.enterBookDetails(FirstPageController.java:39)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  </code></pre>
  
  <p>It seems that LocalSessionFactoryBean is not getting injected, but I cannot figure out the reason.
  Any kind of help would be appreciated.</p>