title: hibernate and spring mvc combination is not working properly
body: <p>I am learning spring mvc with hibernate. I have tried a tutorial where it shows with reverse engineering and hbm.xml. But I want to use annotated domain class for it. For which I am trying some but no luck. I have taken a web project in netbeans with spring and hibernate framework. When I submit the form it work fine and redirect to other page but data is not saved. Can anybody please help me on this please ? here are my attempts below ::</p>
  
  <p>my project structure >>></p>
  
  <p><a href="https://i.stack.imgur.com/GduMo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GduMo.png" alt="enter code here"></a></p>
  
  <p>my dispatcher-servlet.xml >>> </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:p="http://www.springframework.org/schema/p"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
  
      &lt;context:component-scan base-package="com.controller" /&gt;     
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>my 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:p="http://www.springframework.org/schema/p"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>my web.xml >></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&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;2&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;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;redirect.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>my hibernate.cfg.xml >>></p>
  
  <pre><code>    &lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/springhibernate?zeroDateTimeBehavior=convertToNull&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
      &lt;property name="hibernate.jdbc.batch_size"&gt;50&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.query.factory_class"&gt;org.hibernate.hql.ast.ASTQueryTranslatorFactory&lt;/property&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>my User controller >>></p>
  
  <pre><code>    @Controller
  public class UserController {
  
     private static SessionFactory factory;
  
      @RequestMapping(value="/getForm", method = RequestMethod.GET)
      public ModelAndView getDemoForm(){        
          ModelAndView model = new ModelAndView("demoForm");        
          return model;
      }
  
      @RequestMapping(value = "/submitDemoForm.html", method = RequestMethod.POST)
      public ModelAndView submitAdmissionForm(@ModelAttribute("employee") EmployeeAnnotation employee) {
          try {
              factory = new AnnotationConfiguration().
                      configure().
                      //addPackage("com.xyz") //add package if used.
                      addAnnotatedClass(EmployeeAnnotation.class).
                      buildSessionFactory();
          } catch (Throwable ex) {
              System.err.println("Failed to create sessionFactory object." + ex);
              throw new ExceptionInInitializerError(ex);
          }
  
          Session session = factory.openSession();
          Transaction tx = null;
          Integer employeeID = null;
          try {
              tx = session.beginTransaction();
              employeeID = (Integer) session.save(employee);
              tx.commit();
          } catch (HibernateException e) {
              if (tx != null) {
                  tx.rollback();
              }
              e.printStackTrace();
          } finally {
              session.close();
          }
  
          ModelAndView model = new ModelAndView("demoFormSuccess");
  
          return model;
      }
  
  }
  </code></pre>
  
  <p>my EmployeeAnnotation Class (pojo) >>></p>
  
  <pre><code>    import javax.persistence.*;
  
  @Entity
  @Table(name = "employee_annotation")
  public class EmployeeAnnotation {
     @Id @GeneratedValue
     @Column(name = "id")
     private int id;
  
     @Column(name = "first_name")
     private String firstName;
  
     @Column(name = "last_name")
     private String lastName;
  
     @Column(name = "salary")
     private int salary;  
  
     public EmployeeAnnotation() {}
     public int getId() {
        return id;
     }
     public void setId( int id ) {
        this.id = id;
     }
     public String getFirstName() {
        return firstName;
     }
     public void setFirstName( String first_name ) {
        this.firstName = first_name;
     }
     public String getLastName() {
        return lastName;
     }
     public void setLastName( String last_name ) {
        this.lastName = last_name;
     }
     public int getSalary() {
        return salary;
     }
     public void setSalary( int salary ) {
        this.salary = salary;
     }
  }
  </code></pre>
  
  <p>my demoForm.jsp >>></p>
  
  <pre><code>    &lt;form action="/springMvcHibernate/submitDemoForm.html" method="post"&gt;
  
      &lt;p&gt;
        Firts Name : &lt;input type="text" name="firstName"/&gt;
      &lt;/p&gt;
      &lt;p&gt;
        Last Name : &lt;input type="text" name="lastName"/&gt;
      &lt;/p&gt;
      &lt;p&gt;
              Salary : &lt;input type="number" name="salary"/&gt;
      &lt;/p&gt;
      &lt;input type="submit" value="Submit"/&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>my demoFormSuccess.jsp >>></p>
  
  <pre><code>&lt;body&gt;
      &lt;h1&gt;ID is :: ${employee.id}&lt;/h1&gt;
  &lt;/body&gt;
  </code></pre>