Ниже приведена выдержка из моего класса сущностей
открытый класс PartyRole реализует Serializable {
@Id
@GeneratedValue(generator = "IDGenerator", strategy = SEQUENCE)
@SequenceGenerator(name = "IDGenerator", sequenceName = "IDGenerator", allocationSize = 1)
private long id;
Я работаю в Eclipse и использую Derby 10.8.2.2
Я создал последовательность
CREATE SEQUENCE IDGenerator AS BIGINT START WITH 1 INCREMENT BY 1;
Ниже приведен код в моем основном методе.
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();
Когда я закомментирую все вызовы метода persist, кроме первого, все работает нормально.Когда я раскомментирую строки, я получаю следующую ошибку:
[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)
Почему я получаю эту ошибку?Я думаю, что все сделал правильно.
По какой-то причине провайдер JPA (EclipseLink) не получает следующий идентификатор из последовательности.Это ошибка?или я что-то не так делаю?