title: How to prepopulate an object before updating using spring mvc and hibernate?
body: <p>What's the best practice to pre-populate an object before saving this object with hibernate?</p>
  
  <p>What i've done:</p>
  
  <p><strong>My controller:</strong></p>
  
  <pre><code>//The Form
  @RequestMapping(value = "user/{id}/edit", method = RequestMethod.GET)
  public String edit(@PathVariable("id") Long userId, ModelMap modelMap) {
      modelMap.addAttribute("user", userService.find(userId));    
      return "user/userEdit";
  
  }
  //Updating database
  @RequestMapping(value = "user/edit", method = RequestMethod.POST)
  public String update(@ModelAttribute("user") @Valid User user, BindingResult result,
                                                  RedirectAttributes redirectAttrs) {
      if (result.hasErrors()) {
              return "user/userEdit";
      }else{
          userService.update(user);
          redirectAttrs.addFlashAttribute("message", "Success");
  
          return "redirect:user/userEdit";
      }
  }
  </code></pre>
  
  <p>It works if i make a form containing all fields (username, password and id) , but what should i do if i want the user to update only the password?</p>
  
  <p>Since i have a @NotEmpty at username, i get an error that username is null, since its not in the form, but i dont want to put the username field, just the password.</p>
  
  <p><strong>My html form:</strong></p>
  
  <pre><code>&lt;c:url var="url" value="/user/edit" /&gt;
  &lt;form:form method="post" action="${url}" modelAttribute="user" class="form-horizontal"&gt;
      &lt;form:hidden path="id"/&gt;
      &lt;form:hidden path="version"/&gt;
      &lt;fieldset&gt;
          &lt;div class="control-group"&gt;
              &lt;form:label cssClass="control-label" path="password"&gt;&lt;spring:message code="user.label.password"/&gt;: &lt;/form:label&gt;
              &lt;div class="controls"&gt;
                  &lt;form:input cssClass="input-xlarge" path="password" /&gt;
              &lt;/div&gt;
              &lt;form:errors path="password"/&gt;
          &lt;/div&gt;
          &lt;div class="control-group"&gt;
              &lt;form:label cssClass="control-label" path="userRole"&gt;&lt;spring:message code="user.label.role"/&gt;: &lt;/form:label&gt;
              &lt;div class="controls"&gt;
                  &lt;form:select path="userRole"&gt;
                         &lt;form:options items="${userRoleList}" itemValue="id" itemLabel="name"/&gt;
                  &lt;/form:select&gt;
              &lt;/div&gt;
              &lt;form:errors path="userRole"/&gt;
          &lt;/div&gt;
          &lt;div class="control-group"&gt;
              &lt;form:label cssClass="control-label" path="costumer.id"&gt;&lt;spring:message code="user.label.costumer"/&gt;: &lt;/form:label&gt;
              &lt;div class="controls"&gt;
                  &lt;form:select path="costumer.id"&gt;
                         &lt;form:options items="${costumerList}" itemValue="id" itemLabel="name"/&gt;
                  &lt;/form:select&gt;
              &lt;/div&gt;
              &lt;form:errors path="costumer.id"/&gt;
          &lt;/div&gt;
  
          &lt;div class="form-actions"&gt;
              &lt;button type="submit" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
              &lt;a class="btn cancel link" href="&lt;c:url value="/user" /&gt;"&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <ul>
  <li>I tried using @Sessionattributes, but it doesnt work well if i try to
  edit two or more users using browser tabs.</li>
  <li>I tried using property editors, but didnt work with @ModelAtrribute
  User user.</li>
  <li>I tried using convertors but didnt work.</li>
  </ul>
  
  <p>Is the only way to make a User user = userService.find(id) first, and then set the updated values? Something like:</p>
  
  <pre><code>@RequestMapping(value = "user/edit", method = RequestMethod.POST)
  public String update(@RequestParam("password") String password, BindingResult result, RedirectAttributes redirectAttrs) {
      User user = userService.find(id);
  if (password == null{                           
      return "user/userEdit";
  }else{
      user.setPassword("password");
      userService.update(user);
      redirectAttrs.addFlashAttribute("message", "Success");
  
      return "redirect:user/userEdit";
  }
  }
  </code></pre>
  
  <p>Which seens wrong, because there is no validation.</p>