I am using Hibernate 5.2.15 and a H2 database (version 1..4.197). I have created a sequence with
CREATE SEQUENCE SEQ_PRIMARYKEY START WITH 1 INCREMENT BY 1;
and my entity looks like
@Entity
@Table(name="Payment",
uniqueConstraints={@UniqueConstraint(columnNames={"ID"})})
public class Payment {
@Id
@SequenceGenerator(name="seq_PK",sequenceName="SEQ_PRIMARYKEY")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_PK")
@Column(name="ID", nullable=false, unique=true, length=11)
private int id;
...
}
The DAO is implemented as follows
@Override
public void persist(T entity) {
currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
currentTransaction = currentSession.beginTransaction();
currentSession.save(entity);
currentTransaction.commit();
currentSession.close();
}
In the databse the current value of the sequence is for example 190, but when is persist an entity the id is 78. I expect the ID to match the next value of the sequence. Is my expectation wrong? How do I need to change the implementation to meet my expectation?
Edit: When stopping Tomcat, I get the following warning:
WARNUNG: The web application [myApp] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos (Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
Maybe it is somehow related?
Edit2: After dropping the table and the sequence, now it seems to be in sync, but it might be a happy accident only. Although the sequence should start with 1 the first enitiy, which I have persisted got the ID "-46", instead of 1, which I would have expected. Why?