title: Autoincrement version field as part of a composite key
body: <p>I have a table which has a composite primary key</p>
  <pre><code>CREATE TABLE workflow_metadata
  (
    -- other fields here
    version NUMBER(10) NOT NULL,
    workflow_key VARCHAR2(255) NOT NULL,
    CONSTRAINT pk_wf_metadata PRIMARY KEY (version, workflow_key)
  );
  </code></pre>
  <p>And a sequence used for auto incrementing the version field.</p>
  <pre><code>CREATE SEQUENCE workflow_version_seq INCREMENT BY 1 START WITH 1;
  </code></pre>
  <p>Here's the Java code for this entity class. I'm using Spring Data Rest's repository to persist/retrieve entities of this kind.</p>
  <pre><code>@Data
  @Entity
  @SuperBuilder
  @NoArgsConstructor
  @AllArgsConstructor
  @EqualsAndHashCode
  @SequenceGenerator(name = &quot;workflow_version_seq&quot;, allocationSize = 1)
  @IdClass(WorkflowMetadataId.class)
  public class WorkflowMetadata implements Persistable&lt;WorkflowMetadataId&gt; {
      @Id
      private String workflowKey;
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;workflow_version_seq&quot;)
      private Integer version;
  
      // ------
      // Other fields were omitted to keep this sample rather small.
      // ------
  
      // This last part is added for altering the default behaviour of Spring Data Rest, which is updating an entity with same primary key rather than failing when trying to insert it in the DB.
      // https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.entity-persistence.saving-entites.strategies
      // This is just a flag used for holding the new state.
      // This field will not be persisted in the DB.
      @Transient
      private boolean isNew = true;
  
      @Transient
      private WorkflowMetadataId workflowMetadataId = null;
  
      @Override
      public WorkflowMetadataId getId() {
          return new WorkflowMetadataId(workflowKey, version);
      }
  
      @Override
      public boolean isNew() {
          return isNew;
      }
  
      @PrePersist
      @PostLoad
      void markNotNew() {
          this.isNew = false;
      }
  }
  
  
  // this is in a separate file
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  public class WorkflowMetadataId implements Serializable {
      private String workflowKey;
      private Integer version;
  
  }
  </code></pre>
  <p>I'm trying to have the <code>version</code> field auto incremented each time a new entity with the same <code>workflowKey</code> is attempted to be persisted.</p>
  <p>However, what I've done so far guarantees me a unique <code>version</code> field per table. But I would only want a unique <code>version</code> field per each entity that shares the same <code>workflowKey</code>. Is this achievable? Please point me in the right direction.</p>
  <p>Some examples.</p>
  <p>After the following 3 inserts:</p>
  <pre><code>INSERT INTO table workflow_metadata (workflow_key) VALUES ('wf01');
  INSERT INTO table workflow_metadata (workflow_key) VALUES ('wf01');
  INSERT INTO table workflow_metadata (workflow_key) VALUES ('wf02');
  </code></pre>
  <p>What I have in the DB:</p>
  <pre><code>(workflowKey, version)
  wf01, 1
  wf01, 2
  wf02, 3
  
  </code></pre>
  <p>What I want in the DB:</p>
  <pre><code>(workflowKey, version)
  wf01, 1
  wf01, 2
  wf02, 1
  </code></pre>