By default entities annotated with @SequenceGenerator
use initialValue=1 and alocationSize=50.
public @interface SequenceGenerator {
/**
* (Optional) The value from which the sequence object
* is to start generating.
*/
int initialValue() default 1;
/**
* (Optional) The amount to increment by when allocating
* sequence numbers from the sequence.
*/
int allocationSize() default 50;
}
A "sequential" entity id seems to be calculated by EclipseLink with the following formula:
entityId = initialValue - allocationSize + INCREMENT_BY
or in case of using DDL:
entityId = START_WITH - allocationSize + INCREMENT_BY
Going back to your particular cases:
@SequenceGenerator(
name="email-seq-gen",
sequenceName="EMAIL_SEQ_GEN",
allocationSize=500
) // initialValue=1 (default)
CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 1 INCREMENT BY 500;
produces
entityId = 1 - 500 + 1 = -500 // EclipseLink error
@SequenceGenerator(
name="email-seq-gen",
sequenceName="EMAIL_SEQ_GEN",
initialValue=1,
allocationSize=500 )
CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 1 INCREMENT BY 500;
produces
entityId = 1 - 500 + 1 = -500 // EclipseLink error
@SequenceGenerator(
name="email-seq-gen",
sequenceName="EMAIL_SEQ_GEN",
initialValue=500,
allocationSize=500
)
CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 500 INCREMENT BY 500;
produces
entityId = 500 - 500 + 500 = 500 // fine, but inappropriate
entityId = 500 - 500 + 1000 = 1000 // incremented by 500
entityId = 500 - 500 + 1500 = 1500 // incremented by 500
...
To meet your requirements the following one should be used:
@SequenceGenerator(
name="email-seq-gen",
sequenceName="EMAIL_SEQ_GEN",
allocationSize=500
) // initialValue=1 (default) but 'START WITH'=500
CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 500 INCREMENT BY 1;
produces
entityId = 500 - 500 + 1 = 1
entityId = 500 - 500 + 2 = 2
entityId = 500 - 500 + 3 = 3
...
An existing sequence can be removed from the underlying database with the following SQL command:
DROP SEQUENCE email_seq_gen RESTRICT;
I hope it helps.