I noticed a strange problem happening with the application I am writing.
I use Hibernate, Spring and Oracle database. There is a Orders table in the database, which has its PK generated by a sequence. I defined this in the application as follows:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_ORDER_ID")
@SequenceGenerator(name="SEQ_ORDER_ID", sequenceName="SEQ_ORDER_ID", allocationSize=1)
@Column(name = "ID_ORDER", length = 10, nullable = false)
For saving the Order into database, I create a new Order Order order = new Order();
, set its values and use session.persist(order).
Now, the Order object has the correct primary key generated, let's say 120
, while previous one was 119
, but when I look into the database, the newly created order row has primary key value 121
. I used debug and went through the process of creating the Order, that is how I got these values.
If I try creating a new order directly in sqldeveloper, the sequence works correctly and values increment by one.
This happens with every insert, not just Order. The primary key value written into database is consistently +2 instead of +1.
All of the Oracle sequences have the INCREMENT_BY
value 1
.
I tried adding sessionFactory.getCurrentSession().flush();
after the persist, but nothing changes.
Has anyone ever experienced this? Do You have any idea what I might be doing wrong here that causes this behavior?