Calling next value of a sequence in jpa

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-06-18T07:58:43Z Indexed on 2010/06/18 8:03 UTC
Read the original article Hit count: 714

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate