The generated id for a JPA entity generates an "old", used number, when Eclipselink is used as ORM, but with Hibernate the id is the correct next value of the sequence. In the entity class I use these annotations with the id field
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_customer")
@SequenceGenerator(name = "s_customer", sequenceName = "S_CUSTOMER")
private Long id;
I use flyway to setup and populate the db:
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
jdbcTemplate.execute("create sequence S_CUSTOMER");
jdbcTemplate.execute("create table CUSTOMER (" +
"ID BIGINT PRIMARY KEY," +
"DISPLAY_NAME VARCHAR(3000)," +
"VERSION BIGINT" +
")");
}
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
for (int i = 0; i < 10000; i++) {
jdbcTemplate.update(
"insert into CUSTOMER (ID, DISPLAY_NAME, VERSION) " + "select nextval('S_CUSTOMER'), ?, ? ",
"SAP-SE", 0);
}
}
As database I use PostgreSQL.
When my application is going to persist a new entity, using eclipselink, I get an error because the entity got an "old" id:
Call: INSERT INTO CUSTOMER (ID, DISPLAY_NAME, VERSION) VALUES (?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(Customer(id=9959, displayName=Bäckerei Brötchen, version=1)); nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "customer_pkey"
Detail: Key (id)=(9959) already exists.
But when I manually select the current value of the sequence I get the value "10004". So why does eclipselink return a sequence value already used? The same thing with hibernate works just fine.