title: Calling next value of a sequence in jpa
body: <p>I have a class mapped as an Entity to persist it in a database. I have an id field as the primary key so every time the object is persisted the value of the id is retrieved from the sequence "myClass_pk_seq", a code like the following one.</p>
  
  <pre><code>@Entity
  @Table(name="myObjects")
  public class MyClass {
      @Id
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
      @SequenceGenerator(name="sequence", sequenceName="myClass_pk_seq", allocationSize=1)
      @Column(name="myClassId")
      private Integer id;
  
      private Integer code;
  
  
      ...
  }
  </code></pre>
  
  <p>I need to make in "code" attribute something similar to id. I need to have a sequence so I can assign to code the next value of the sequence (to reserve that value in case the user wants to reserve it but without persisting data). I mean the user will see the field, if he doesn't know what to enter he can push a button and receieve in the screen the next possible value (and he can or not accept it). How can I get the next value of a sequence defined in JPA (and increment its value) without persisting the data for a non primary key field?</p>
  
  <p>I just want to have a method which call nextval on a sequence associated with "code" field, and returns the value. What's the best way to do it in JPA with annotations?</p>
  
  <p>Thanks.</p>