title: @Version column is not working out of the box with spring data jdbc
body: <p>I have my version column defined like this</p>
  
  <pre><code>@org.springframework.data.annotation.Version
  protected long version;
  </code></pre>
  
  <p>With Spring Data JDBC it's always trying to INSERT. Updates are not happening. When I debug I see that, <code>PersistentEntityIsNewStrategy</code> is being used which is the default strategy. It has <code>isNew()</code> method to determine the state of the entity being persisted. I do see that version and id are used for this determination. </p>
  
  <p>But my question is who is responsible to increment the version column after every save, so that when the second time <code>.save()</code> is called, the <code>isNew()</code> method can return false.</p>
  
  <p>Should we do fire a <code>BeforeSaveEvent</code> and handle the incrementation of Version column? Would that be good enough to handle the <code>OptimisticLock</code> ?</p>
  
  <p><strong>Edit</strong>
  I added an ApplicationListener to listen to BeforeSaveEvent like this.</p>
  
  <pre><code>public ApplicationListener&lt;BeforeSaveEvent&gt; incrementingVersion() {
      return event -&gt; {
          Object entity = event.getEntity();
          if (BaseDataModel.class.isAssignableFrom(entity.getClass())) {
              BaseDataModel baseDataModel = (BaseDataModel) entity;
              Long version = baseDataModel.getVersion();
              if (version == null) {
                  baseDataModel.setVersion(0L);
              } else {
                  baseDataModel.setVersion(version + 1L);
              }
          }
      };
  }
  </code></pre>
  
  <p>So now the version column works, but rest of Auditable fields <code>@CreatedAt, @CreatedBy,@LastModifiedDate and @LastModifiedBy</code> are not set!! </p>
  
  <p><strong>Edit2</strong></p>
  
  <p>Created a new ApplicationListener like below. In this case both my custom listener and Spring's RelationalAuditingListener are getting called. But still it doesn't solve the problem. Because the order of listeners[custom one followed by spring's] making the <code>markAudited</code> to invoke <code>markUpdated</code> instead of <code>markCreated</code>, since the version column is already incremented. I tried to make my Listener be the <code>LOWEST_PRECEDENCE</code> still no luck. </p>
  
  <p>My custom listener here</p>
  
  <pre><code>public class CustomRelationalAuditingEventListener
      implements ApplicationListener&lt;BeforeSaveEvent&gt;, Ordered {
  
  @Override
  public void onApplicationEvent(BeforeSaveEvent event) {
  
      Object entity = event.getEntity();
      // handler.markAudited(entity);
  
      if (BaseDataModel.class.isAssignableFrom(entity.getClass())) {
          BaseDataModel baseDataModel = (BaseDataModel) entity;
  
          if (baseDataModel.getVersion() == null) {
              baseDataModel.setVersion(0L);
          } else {
              baseDataModel.setVersion(baseDataModel.getVersion() + 1L);
          }
      }
  }
  
  @Override
  public int getOrder() {
      return LOWEST_PRECEDENCE;
  }
  </code></pre>
  
  <p>}</p>