3

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

0

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think that this is the cause here - EclipseLink verifies that allocationSize and INCREMENT BY matches and will throw an exception which explicitly states that if this is not case
There is an error in your example, allocationSize should be on SequenceGenerator not on GeneratedValue
@bjartek Thanks, Example has been fixed.
I used the same, but still I am getting an increment of 2 in Id instead of 1. Any idea?
1

Change allocationSize=1 by allocationSize=2, it is a Eclipselink bug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.