Search code examples
hibernatejpajpa-2.0hibernate-mappinghibernate-jpa

Hiberate JPA - sequences becoming negative when record is inserted


The environment:

Database - Oracle 12c
Hibernate - 5.2.10Final 
JPA - version 2.0

We are using entityManager.merge to insert the object. The value of the sequence is a positive value but when inserted in db, it becomes negative. Can't seem to find the cause of this.

Here is the hibernate setting we have for our entity object id attribute field, with the sequence setting

@Id
@SequenceGenerator(name = "someSequence", sequenceName = "SOME_SEQUENCE")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "someSequence")
@Column(name = "RECORD_ID", unique = true, nullable = false, precision = 10, scale = 0) 
private String recordId;

anyone can help? thanks.


Solution

  • In SequenceGenerator annotation there is a variable named allocationSize. This size is by default is 50. It means that Hibernate will reserve 50 ids and it will not need to retrieve new id for every insert. So you need:

    • set allocationSize to the same value like in your someSequence incrementBy value.
    • or set incrementBy value on your someSequence to match allocationSize value.

    Then you problems will gone.