Calling next value of a sequence in jpa
- by Javi
Hello,
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.
@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;
...
}
I need to get the next value of the sequence myClass_pk_seq to reserve that value (get the next value of the sequence and the current value is incremented), but without saving the object. How can I call the next value of a sequence when it's defined like this?
Thanks.