title: Cascade optimistic locking to @OneToMany relationships
body: <p>I am trying to check if an entity being edited was changed by another source. I managed to detect changes in the @ManyToOne relationships and primitive fields, however the collections were not affected:</p>
  
  <pre><code>@OptimisticLocking(type = OptimisticLockingType.ALL_COLUMNS)
  @Entity(name = "RNC_RELATORIO")
  public class RncRelatorio implements Serializable{
  
  @ManyToOne
  @JoinColumn(name = "emitente_id")
  private SosUsuario emitente;
  
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "relatorio", orphanRemoval = true)
  private List&lt;RncAcao&gt; acoes = new ArrayList&lt;&gt;();
  
  ...
  </code></pre>
  
  <p>I found some articles about using versioning by creating another field in the table that gets incremented after every update. Is there another way? If not, where should the version column be placed? </p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I added a version column in the RNC_RELATORIO table and mapped it in the RncRelatorio entity with the @Version annotation. Also changed the @OptimisticLocking type and the cascade value. Now everytime i am editing an object and i try to update it, i get an exception if the version from the database table is not equal to the one object's one. This happens even if nothing was changed in the entity during the update, but that's not a problem. </p>
  
  <pre><code>@OptimisticLocking(type = OptimisticLockingType.VERSION_COLUMN, cascade = true)
  @Entity(name = "RNC_RELATORIO")
  public class RncRelatorio implements Serializable{
  
     @Version
     private int version;
  
     public int getVersion() {
         return version;
     }
  
     public void setVersion(int version) {
         this.version = version;
     }
     ...
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>When i try to get the sysdate from Oracle database i get the optimistic locking exception after the database was changed somewhere else even though i am just running a select through the RncRelatorio DAO and not trying to update the table row. Any idea why?</p>
  
  <pre><code>public class GenericDAO&lt;T&gt; implements Dao&lt;T&gt; {
  
     private final Class&lt;T&gt; clazz;
     private final EntityManager em;
  
     public GenericDAO(Class&lt;T&gt; _clazz) {
         this.clazz = _clazz;
         this.em = PersistenceFactory.getInstance();
     }
  
     @Override
     public Date getSysdate() {
        Query q = em.createNativeQuery("select sysdate from dual");
        List&lt;Timestamp&gt; l = q.getResultList();
        Date d = new Date(l.get(0).getTime());
  
        return d;
     }
  }
  
  
  public class RncRelatorioDao extends GenericDAO&lt;RncRelatorio&gt;{
  
     public RncRelatorioDao() {
       super(RncRelatorio.class);
     }
  }
  </code></pre>