title: Sping MVC and hibernate: undeclared variable in jsp become null when saved
body: <p>I just created my simple web application using Spring frameworks and hibernate. But I face the crucial problem when there is a transaction with my database. 
  When I tried to update my object, undeclared variables become null when saved to the database.</p>
  
  <p>I will explain it with an example.</p>
  
  <p>Model.java:</p>
  
  <pre><code>public class Model implements Serializable {
     private int id;
     private String firstName;
     private String lastName;
     private String description;
  
     //setter getter
  }
  </code></pre>
  
  <p>ModelDaoImpl.java:</p>
  
  <pre><code>@Repository("modelDao")
  public class ModelDaoImpl implements ModelDao {
     @Autowired
     SessionFactory sessionFactory;
     public Model create (Model model) throws Exception {
        this.sessionFactory.getCurrentSession().save(model);
        return model;
     }
     public Model update (Model model) throws Exception {
        this.sessionFactory.getCurrentSession().update(model);
        return model;
     }
     public Model get (Serializable id) throws Exception {
        return (Model) this.sessionFactory.getCurrentSession().get(Model.class, id);
     }
  
  }
  </code></pre>
  
  <p>ModelServiceImpl.java:</p>
  
  <pre><code>@Service("modelService")
  public class ModelServiceImpl implements ModelService {
     @Autowired
     modelDao modelDao;
     @Transactional
     public Model create (Model model) throws Exception {
        return modelDao.create(model);
     }
     @Transactional
     public Model update (Model model) throws Exception {
        return modelDao.udpdate(model);
     }
     @Transactional
     public Model get (Serializable id) throws Exception {
        return modelDao.get(id);
     }
  </code></pre>
  
  <p>}</p>
  
  <p>ModelController.java:</p>
  
  <pre><code>@Controller
  public class ModelController {
     @Autowired
     ModelService modelService
     @RequestMapping(value="/editModel", method.RequestMethod.GET)
     public String formCreator(ModelMap model, HttpServletRequest request) {
        Integer id = new Integer(request.getParameter("id"));
        Model modelData = new Model();
        if (id != null) {
           modelData = modelService.get(id);
        }
        model.addAttribute("modelData", modelData);
        return "editModel"
     }
     @RequestMapping(value="/saveModel", method.RequestMethod.POST)
     public String saveData(@ModelAttribute("modelData") Model modelData) {
        if (modelData.getId() == null) {
           modelService.create(modelData);
        } else {
           modelService.update(modelData);
        }
        return "redirect:/modelList";
     }
  
  //SKIP FOR GET MODEL LIST AND GET MODEL DETAIL
  }
  </code></pre>
  
  <p>editModel.jsp:</p>
  
  <pre><code>&lt;form:form action="${pageContext.request.contextPath}/saveModel" method="POST" modelAttribute="modelData"&gt;
  &lt;table&gt;
     &lt;form:hidden path="id"/&gt;
     &lt;tr&gt;
        &lt;td&gt;First Name:&lt;/td&gt; 
        &lt;td&gt;&lt;form:input path="firstName"/&gt;&lt;/td&gt; 
     &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;Last Name:&lt;/td&gt;
        &lt;td&gt;&lt;form:input path="lastName"/&gt;&lt;/td&gt; 
     &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td colspan="2" align="center"&gt;&lt;input type="submit" value="Save"&gt;&lt;/td&gt;
     &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>From jsp page we see that I didn't declare variable 'description', but when update my existing data the variable 'description' become null. I have tried using @DynamicUpdate or related to dynamic update process, but the result was still the same, the variable 'description' still become null.</p>
  
  <p>Any suggestion?</p>