following is an excerpt from my entity class
public class PartyRole implements Serializable {
@Id
@GeneratedValue(generator = "IDGenerator", strategy = SEQUENCE)
@SequenceGenerator(name = "IDGenerator", sequenceName = "IDGenerator", allocationSize = 1)
private long id;
I am working in Eclipse and I am using Derby 10.8.2.2
I have created a Sequence
CREATE SEQUENCE IDGenerator AS BIGINT START WITH 1 INCREMENT BY 1;
following is the code in my main method.
EntityManagerFactory factory = Persistence.createEntityManagerFactory("notesJPA");
EntityManager entityManager = factory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(actor);
//entityManager.persist(director);
//entityManager.persist(castingManager);
//entityManager.persist(producer);
//entityManager.persist(screenWriter);
//entityManager.persist(storyWriter);
tx.commit();
When I comment out all calls to persist method except the first, things work fine. When I uncomment the lines, I get the following error.
[EL Warning]: 2012-03-07 18:57:03.098--UnitOfWork(2079310344)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL120307183732430' defined on 'PARTYROLE'.
Error Code: -1
Call: INSERT INTO PARTYROLE (ID, NAME, DTYPE) VALUES (?, ?, ?)
bind => [0, CastingManager, CastingManager]
Query: InsertObjectQuery(com.ats.data.partyrole.CastingManager@72f6f1b6)
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL120307183732430' defined on 'PARTYROLE'.
Error Code: -1
Call: INSERT INTO PARTYROLE (ID, NAME, DTYPE) VALUES (?, ?, ?)
bind => [0, CastingManager, CastingManager]
Query: InsertObjectQuery(com.ats.data.partyrole.CastingManager@72f6f1b6)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at amrite.com.jpa.test.LoadPartyRoles.main(LoadPartyRoles.java:38)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL120307183732430' defined on 'PARTYROLE'.
Error Code: -1
Call: INSERT INTO PARTYROLE (ID, NAME, DTYPE) VALUES (?, ?, ?)
bind => [0, CastingManager, CastingManager]
Query: InsertObjectQuery(com.ats.data.partyrole.CastingManager@72f6f1b6)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:801)
Why am I getting this error? I think I have done all the right things.
For some reason the JPA provider (EclipseLink) is not picking up the next id from the Sequence. Is this a bug? or something I am doing wrong?