title: Hibernate JPA Sequence (non-Id)
body: <p>Is it possible to use a DB sequence for some column that <strong>is not the identifier/is not part of a composite identifier</strong>? </p>
  
  <p>I'm using hibernate as jpa provider, and I have a table that has some columns that are generated values (using a sequence), although they are not part of the identifier.</p>
  
  <p>What I want is to use a sequence to create a new value for an entity, where the column for the sequence is <strong>NOT</strong> (part of) the primary key:</p>
  
  <pre><code>@Entity
  @Table(name = "MyTable")
  public class MyEntity {
  
      //...
      @Id //... etc
      public Long getId() {
          return id;
      }
  
     //note NO @Id here! but this doesn't work...
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "myGen")
      @SequenceGenerator(name = "myGen", sequenceName = "MY_SEQUENCE")
      @Column(name = "SEQ_VAL", unique = false, nullable = false, insertable = true, updatable = true)
      public Long getMySequencedValue(){
        return myVal;
      }
  
  }
  </code></pre>
  
  <p>Then when I do this:</p>
  
  <pre><code>em.persist(new MyEntity());
  </code></pre>
  
  <p>the id will be generated, but the <code>mySequenceVal</code> property will be also generated by my JPA provider.</p>
  
  <p>Just to make things clear: I want <strong>Hibernate</strong> to generate the value for the <code>mySequencedValue</code> property. I know Hibernate can handle database-generated values, but I don't want to use a trigger or any other thing other than Hibernate itself to generate the value for my property. If Hibernate can generate values for primary keys, why can't it generate for a simple property?</p>