How to map an Oracle Sequence with JPA

This tutorial describes how to map an Oracle Sequence with JPA.

Supposing that you have just created an Oracle sequence using the command:

CREATE SEQUENCE AGENDA_SEQ;

Then, you can use this sequence in your Entity as follows:

@Entity 
public class Agenda {
     

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="AGENDA_SEQ", allocationSize = 1)
private Long id;

}

The @SequenceGenerator annotation is used to define a sequence and accepts a name, an initial value (the default is 1) and an allocation size (the default is 50). A sequence is global to the application and can be used by one or more fields in one or more classes