JPA Eclipselink getting wrong sequence number
eclipselink, java, jpa, sequence
Solution
When you created the sequence you specified a size to increment by. For example this sequence increments by 1.
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
When using the `SequenceGenerator` annotation in JPA you can specify an allocation size.
@Id
@SequenceGenerator(name = "SEQ_RULES", sequenceName = "SEQUENZ_RULES",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_RULES")
@Column(name = "SERIALNO")
protected Long serialno;
Make sure the allocation size and the increment match between JPA and the Sequence DDL.
Problem
I'm using some Entities mapped to Oracle DB-Tables. For ID-Generation I'm using a sequence generator annotated as following: ``` @Id @SequenceGenerator(name = "SEQ_RULES", sequenceName = "SEQUENZ_RULES") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_RULES") @Column(name = "SERIALNO") protected Long serialno; ``` During programm execution I make a new Instance from my Entity and want to persist this generated one. After restart of the database I'm getting wrong sequence numbers through JPA-EclipseLink, but not through the Console directly on the database. I turned on following properties in the persistence.xml toget also the binding parameters used in the generated statements. ``` <properties> <property name="eclipselink.logging.level" value="FINE"/> <property name="eclipselink.logging.parameters" value="true"/> </properties> ``` For example: If I generate a new instance of my entity and want to persist this one I get 2717 for serialNo and if I execute ``` SELECT SEQUENZ_RULES.NEXTVAL FROM DUAL ``` I get 2767 as nextval. The problem is that the JPA-generated serialNo must be unique, and now I stil have some datasets with this serialNo. Im getting an exception: ``` java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (JASON.SYS_C0084866) violated ``` Non Is there any caching through eclipse which is affecting the Sequence Generation or what could be the error for this? Used Components: GlassFish 3.1.1 EclipseLink 2.3.0.v20110604-r9504 Database: Oracle Version: Oracle Database 11g Release 11.1.0.7.0 - 64bit Driver: Oracle JDBC driver Version: 11.2.0.1.0