title: Is it possible to update only a subset of attributes on an entity using Spring MVC with JPA?
body: <p>I'm working with Spring Roo, using Spring MVC and JPA for persistence with a MySQL database. I'm very new to Spring MVC and Java in general but have worked with CakePHP and Rails.</p>
  
  <p>I have a <code>User</code> entity that contains personal details in addition to a password. Something like this (excluding a lot of Roo-generated functionality in additional .aj files):</p>
  
  <pre><code>public class User {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "ID")
      private Long id;
  
      @Column(name = "PASSWORD", length = 32)
      private String password;
  
      @Column(name = "FIRST_NAME", length = 25)
      private String firstName;
  
      @Column(name = "LAST_NAME", length = 25)
      private String lastName;
  
      @Column(name = "ADDRESS", length = 255)
      private String address;
  
      // The appropriate getters and setters
      ...
  }
  </code></pre>
  
  <p>Then I have an edit action in my <code>User</code> controller that I created following conventions from Roo's auto-generated scaffolding:</p>
  
  <pre><code>@RequestMapping(value="/edit", method = RequestMethod.GET)
  public String editForm(Model uiModel) {
      String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      uiModel.addAttribute("user", User.findUserByUsername(username).getSingleResult());
      return "account/edit";
  }
  </code></pre>
  
  <p>And a JSPX view to render the form, again following Roo's conventions:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"&gt;
      &lt;jsp:directive.page contentType="text/html;charset=UTF-8"/&gt;
      &lt;jsp:output omit-xml-declaration="yes"/&gt;
      &lt;form:update id="" label="Personal Details" modelAttribute="user" path="/account" versionField="none"&gt;
          &lt;field:input id="" field="firstName" label="First Name" /&gt;
          &lt;field:input id="" field="lastName" label="Last Name" /&gt;
          &lt;field:textarea id="" field="address" label="Street Address" /&gt;
      &lt;/form:update&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I do <em>not</em> want the form to update the password, just the provided fields (first name, last name, and address).</p>
  
  <p>The update action, again following Roo convention:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
  public String edit(@Valid User user, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
      if (bindingResult.hasErrors()) {
          uiModel.addAttribute("user", user);
          return "account/edit";
      }
      uiModel.asMap().clear();
      user.merge();
      return "redirect:/account";
  }
  </code></pre>
  
  <p>The user object is updated perfectly, but the problem is that it overwrites the password field with null because it's not provided as an input in the form, and thus set to null in the User object passed to the form submit request handler. The problem doesn't show up with the Roo generated scaffolding because they provide form inputs for all of the columns. So I could add it as a hidden field but that doesn't sound like a good idea. And I get the feeling there's a much better way to do it...</p>
  
  <hr>
  
  <h2><strong>TL;DR How can I update only the entity attributes provided in a form without overwriting the other attributes?</strong></h2>
  
  <p>In other words, how can I make Spring/JPA generate the SQL</p>
  
  <pre><code>UPDATE user SET firstname=?, lastname=?, address=?
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>UPDATE user SET firstname=?, lastname=?, address=?, password=?
  </code></pre>
  
  <p>Code samples would be fantastic since I'm new to all of this :)</p>
  
  <hr>
  
  <p>Thank you!</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> I was able to make it work using yglodt's suggestion, adding the following method to my User model:</p>
  
  <pre><code>@Transactional
  public void mergeWithExistingAndUpdate() {
      final User existingUser = User.findUser(this.getId());
  
      existingUser.setFirstName(this.getFirstName());
      existingUser.setLastName(this.getLastName());
      existingUser.setAddress(this.getAddress());
  
      existingUser.flush();
  }
  </code></pre>
  
  <p>and calling that from my controller action instead of user.merge():</p>
  
  <pre><code>user.mergeWithExistingAndUpdate();
  </code></pre>