title: Using @ModelAttribute in spring hibernate
body: <p>Getting error while using @ModelAttribute, tried many of the solutions but could not find any answers</p>
  
  <p>My controller is</p>
  
  <pre><code>@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST)
  public String postAddUser(
          @RequestParam("name") String name,
          @RequestParam("role") Role role,
          @RequestParam("email_address") String emailAddress
          ) {
      System.out.println("Inside postAddUser");
      User user = new User(name, role,emailAddress);
      user = userRepository.save(user);
      return "redirect:user?id=" + user.getId();
  }
  </code></pre>
  
  <p>My view page is:</p>
  
  <pre><code>&lt;title&gt;Add User&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-12"&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;h2&gt;Add User&lt;/h2&gt;
                      &lt;form action="user" method="post" role="form"&gt;
  
  
                          &lt;div class="form-group "&gt;
                              &lt;label for="user"&gt;User Name:&lt;/label&gt; 
                              &lt;input type="text"
                                  class="form-control" name="name" id="name"
                                  placeholder="Enter User Name" required&gt;
                          &lt;/div&gt;
  
  
                          &lt;div class="form-group"&gt;
                              &lt;label for="email"&gt;Email Address:&lt;/label&gt; &lt;input type="email"
                                  name="email_address" class="form-control" id="email"
                                  placeholder="Enter email Address" required&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group"&gt;
                              &lt;label for="role"&gt;Role:&lt;/label&gt; 
                              &lt;select name="role"
                                  class="form-control" id="role" placeholder="Enter Role" required&gt;
                                  &lt;option disabled selected&gt;&lt;/option&gt;
                                  &lt;c:forEach var="role" items="${roles}"&gt;
                                      &lt;option value="${role.value}"&gt;${role.name}&lt;/option&gt;
                                  &lt;/c:forEach&gt;
                              &lt;/select&gt;
                          &lt;/div&gt;
  
                          &lt;button type="submit" value="Submit" name="add"
                              class="btn btn-info active"&gt;Submit&lt;/button&gt;
                          &lt;a href="adminHome" class="btn btn-info" role="button"&gt;Back&lt;/a&gt;
  
                      &lt;/form&gt;
  
  
                  &lt;/div&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;div class="col-md-4"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>User Bean</p>
  
  <pre><code>@Entity
  @Table(name="USER")
  public class User extends BaseEntity{
  
      @Column(name="NAME")
      private String name;
  
      @Column(name="ROLE")
      private Role role;
  
      @Column(name = "EMAIL_ADDRESS", nullable = false)
      private String emailAddress;
  
      public User(){}
  
  
  
      public User(String name, Role role, String emailAddress) {
          super();
          this.name = name;
          this.role = role;
          this.emailAddress = emailAddress;
      }
  
  
  
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public Role getRole() {
          return role;
      }
      public void setRole(Role role) {
          this.role = role;
      }
  
      public String getEmailAddress() {
          return emailAddress;
      }
  
      public void setEmailAddress(String emailAddress) {
          this.emailAddress = emailAddress;
      }
  
  
  
  }
  </code></pre>
  
  <p>I tried using @ModelAttribute in my controller but somehow value for email is null in when it comes to controller side.</p>
  
  <p>Below is code for same after changing my code to fit @ModelAttribute.</p>
  
  <p>Controller side.</p>
  
  <pre><code>@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST)
      public String postAddUser(@ModelAttribute("user")User user) {
          System.out.println("Inside postAddUser");
          System.out.println(user.getId());
          System.out.println(user.getEmailAddress());
          System.out.println(user.getName());
          System.out.println(user.getRole());
          user = userRepository.save(user);
          return "redirect:user?id=" + user.getId();
      }
  </code></pre>
  
  <p>View page</p>
  
  <pre><code>&lt;title&gt;Add User&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-12"&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;h2&gt;Add User&lt;/h2&gt;
                      &lt;form action="user" method="post" role="form"&gt;
  
  
                          &lt;div class="form-group "&gt;
                              &lt;label for="user"&gt;User Name:&lt;/label&gt; 
                              &lt;input type="text"
                                  class="form-control" name="name" id="name"
                                  placeholder="Enter User Name" value="${user.name}" required&gt;
                          &lt;/div&gt;
  
  
                          &lt;div class="form-group"&gt;
                              &lt;label for="email"&gt;Email Address:&lt;/label&gt; &lt;input type="email"
                                  name="email_address" class="form-control" id="email"
                                  placeholder="Enter email Address" value="${user.emailAddress}" value="${role.value}" required&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group"&gt;
                              &lt;label for="role"&gt;Role:&lt;/label&gt; 
                              &lt;select name="role"
                                  class="form-control" id="role" placeholder="Enter Role" required&gt;
                                  &lt;option disabled selected&gt;&lt;/option&gt;
                                  &lt;c:forEach var="role" items="${roles}"&gt;
                                      &lt;option value="${role.value}"&gt;${role.name}&lt;/option&gt;
                                  &lt;/c:forEach&gt;
                              &lt;/select&gt;
                          &lt;/div&gt;
  
                          &lt;button type="submit" value="Submit" name="add"
                              class="btn btn-info active"&gt;Submit&lt;/button&gt;
                          &lt;a href="adminHome" class="btn btn-info" role="button"&gt;Back&lt;/a&gt;
  
                      &lt;/form&gt;
  
  
                  &lt;/div&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;div class="col-md-4"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Below is the error I am getting </p>
  
  <pre><code>Inside postAddUser
  0
  null
  Harding Berg
  HEAD_OF_DEPARTMENT
  Mar 10, 2016 2:49:33 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet spring threw exception
  org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10097 table: USER column: 
  EMAIL_ADDRESS
      at org.hsqldb.error.Error.error(Unknown Source)
      at org.hsqldb.Table.enforceRowConstraints(Unknown Source)
      at org.hsqldb.Table.insertSingleRow(Unknown Source)
      at org.hsqldb.StatementDML.insertRowSet(Unknown Source)
      at org.hsqldb.StatementInsert.getResult(Unknown Source)
      at org.hsqldb.StatementDMQL.execute(Unknown Source)
      at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
      at org.hsqldb.Session.execute(Unknown Source)
      at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
      at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
      at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:203)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHa
  ndler.java:122)
      at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
      at com.sun.proxy.$Proxy58.executeUpdate(Unknown Source)
      at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2870)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3381)
      at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
      at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
      at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
      at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
      at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
      at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71)
      at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236)
      at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216)
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
      at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900)
      at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:884)
      at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)
      at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEnt
  ityManagerCreator.java:365)
      at com.sun.proxy.$Proxy43.merge(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityMan
  agerCreator.java:240)
      at com.sun.proxy.$Proxy43.merge(Unknown Source)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:329)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMe
  thodOn(RepositoryFactorySupport.java:368)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(Re
  positoryFactorySupport.java:349)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationI
  nterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at com.sun.proxy.$Proxy51.save(Unknown Source)
      at in.ac.jmi.controllers.AdminController.postAddUser(AdminController.java:87)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocab
  leHandlerMethod.java:96)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMapp
  ingHandlerAdapter.java:617)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHa
  ndlerAdapter.java:578)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:8
  0)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.j
  ava:113)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>