title: Spring JPA - RESTful partial update and validation for entity
body: <p>I've a simple RESTful API based on Spring MVC using a JPA connected MySQL database. Until now this API supports complete updates of an entity only. This means all fields must be provided inside of the request body.</p>
  
  <pre><code>@ResponseBody
  @PutMapping(value = "{id}")
  public ResponseEntity&lt;?&gt; update(@Valid @RequestBody Article newArticle, @PathVariable("id") long id) {
  
      return service.updateById(id, newArticle);
  }
  </code></pre>
  
  <p>The real problem here is the validation, how could I validate only provided fields while still require all fields during creation?</p>
  
  <pre><code>@Entity
  public class Article {
  
      @NotEmpty @Size(max = 100) String title;
      @NotEmpty @Size(max = 500) String content;
  
      // Getters and Setters
  }
  </code></pre>
  
  <p>Example for a partial update request body <code>{"content": "Just a test"}</code> instead of <code>{"title": "Title", "content": "Just a test"}</code>.
  The actual partial update is done by checking if the given field is not null:</p>
  
  <pre><code>if(newArticle.getTitle() != null) article.setTitle(newArticle.getTitle());
  </code></pre>
  
  <p>But the validation of course wont work! I've to deactivate the validation for the update method to run the RESTful service. I've essentially two questions:</p>
  
  <ul>
  <li>How can I validate only a "existing" subset of properties in the
  update method while <strong>still require</strong> all fields during creation?</li>
  <li>Is there a more elegant way for update partially then checking for
  null?</li>
  </ul>