title: Correct way of finding what was modified by a post in an spring-mvc controller?
body: <p>It is a rather general question, but I will give a stripped down example. Say I have a Web CRUD application that manages simple entities stored in a database, nothing but classic : JSP view, RequestMapping annotated controller, transactional service layer and DAO.<br>
  On an update, I need to know the previous values of my fields, because a business rule asks a for a test involving the old and new values.</p>
  
  <p>So I am searching for a best practice on that use case.</p>
  
  <p>I thing that spring code is way more extensively tested and more robust than my own, and I would like to do it <em>the spring way</em> as much as possible.</p>
  
  <p>Here is what I have tried :</p>
  
  <p>1/ load an empty object in controller and manage the update in service :<br>
  Data.java:</p>
  
  <pre><code>class Data {
      int id; // primary key
      String name;
      // ... other fields, getters, and setters omitted for brevity
  }
  </code></pre>
  
  <p>DataController</p>
  
  <pre><code>...
  @RequestMapping("/data/edit/{id}", method=RequestMethod.GET)
  public String edit(@PathVariable("id") int id, Model model) {
      model.setAttribute("data", service.getData(id);
      return "/data/edit";
  }
  @RequestMapping("/data/edit/{id}", method=RequestMethod.POST)
  public String update(@PathVariable("id") int id, @ModelAttribute Data data, BindingResult result) {
      // binding result tests omitted ..
      service.update(id, data)
      return "redirect:/data/show";
  }
  </code></pre>
  
  <p>DataService</p>
  
  <pre><code>@Transactional
  public void update(int id, Data form) {
      Data data = dataDao.find(id);
      // ok I have old values in data and new values in form -&gt; do tests stuff ...
      // and MANUALLY copy fields from form to data
      data.setName(form.getName);
      ...
  }
  </code></pre>
  
  <p>It works fine, but in real case, if I have many domain objects and many fields in each, it is quite easy to forget one ... when spring <code>WebDataBinder</code> has done it including validation in the controller without I have to write any single thing other than <code>@ModelAttribute</code> !</p>
  
  <p>2/ I tried to preload the Data from the database by declaring a Converter<br>
  DataConverter</p>
  
  <pre><code>public class DataConverter&lt;String, Data&gt; {
      Data convert(String strid) {
          return dataService.getId(Integer.valueOf(strid));
      }
  }
  </code></pre>
  
  <p>Absolutely magic ! The <code>data</code> if fully initialized from database and fields present in form are properly updated. But ... no way to get the previous values ...</p>
  
  <p>So my question is : what could be the way  to use spring DataBinder magic <strong>and</strong> to have access to previous values of my domain objects ?</p>