Spring Data JPA Hibernate Implementing Optimistic Locking on DTOS
<p>Every time I save my a data from the form. It will always come first from the DTO. </p>

<p>So from my thymeleaf template/Controller. I use this method to save my entity</p>

<pre><code>@RequestMapping(value = "/save",method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute CollectionDTO collectionDTO){
    Collection collection = collectionService.save(collectionDTO);
    return new ModelAndView("redirect:/collection/view/"+collection.getId());
}
</code></pre>

<p>And this is the CollectionService.save</p>

<pre><code>@Override
public Collection save(CollectionDTO collectionDTO) {
    Collection collection;
    if(collectionDTO.getId() != null){
        collection = collectionRepo.findOne(collectionDTO.getId());
        mapper.map(collectionDTO, collection);
    }else{
        collection = mapper.map(collectionDTO,Collection.class);

    }
    collection = collectionRepo.save(collection);
    return collection;
}
</code></pre>

<p>The mapper.map basically maps the contents from the DTO to the Model.</p>

<p>The Collection Entity is a little bit like this</p>

<pre><code>public class Collection{

  @Version
  private long version;
}
</code></pre>

<p>Have I missed anything?</p>