Close

Sequence generation in JPA

@Entity
@Table(name = “Facility”)
public class FacilityEntity implements Serializable{

private static final long serialVersionUID = -5193920209461702399L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “SEQ_FACILITY_ID”)
@SequenceGenerator(name = “SEQ_FACILITY_ID”, sequenceName=”SEQ_FACILITY_ID”)
@Column(name=”facilityid”)
private Long facilityId;
..

}

It took me some time to figure this out, you have to have sequenceName in your SequenceGenrator annotation. It might work without it, and might as well give you sequence does not exit problem. The later case in which it is hard to trace what happened.

To make sure you have enough privileges on your sequence in oracle, you can test getting next generated value by using the following command in oracle:

SQL> connect shahin/shahin
Connected.
SQL> select shahin.seq_facility_id.nextval from dual;

NEXTVAL
———-
1

if it doesn’t show the next available value in the system, you need to login using the sysdba credentials and grant access to that sequence for that user. As follow:

SQL> connect / as sysdba
Connected.
SQL> grant select any sequence to shahin;

Grant succeeded.

Leave a Reply

Your email address will not be published. Required fields are marked *