How to update only attributes that have changed - Spring MVC
<p>My controller</p>

<pre><code>@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String updateUserById(@PathVariable("id") Long id, Model model) {
    User user = userRepository.findOne(id);
    model.addAttribute(user);
    return "admin/editUser";
}

@RequestMapping(value = "/user/{id}", method = RequestMethod.POST)
@ResponseBody
public String updateUserById(@PathVariable("id") Long id, @ModelAttribute User user) {
    userRepository.updateUser(id, user); // with a try catch
}
</code></pre>

<p>The dao</p>

<pre><code>@Override
public void updateUser(Long id, User user) {
    User userDB = userRepository.findOne(id);
    userDB.setFirstName(user.getFirstName());
    userDB.setLastName(user.getLastName());
    userDB.setEmail(user.getEmail());
    userDB.setUsername(user.getUsername());
    userRepository.save(userDB);
}
</code></pre>

<p>This method works but it's pretty ugly for me. Let's say that the user have just changed the firstname field in the view, how can I adapt my code to only call the function to set the firstname ?</p>

<p>Something like the Observer pattern to notify field that have change ?</p>