Hibernate Generated Value strategy

21,983

Solution 1

Database do not care if there is holes in the sequence. Also in general it is possible and most likely easier to change application design so that it does not expect list of id values to not contain holes.

If some odd reason forces such a design, you have to use custom generator. Implementation details can be found via this question: Hibernate ID Generator

Solution 2

I don't think there is any such optio available in hibernate. Instead of AUTO,you can try the following strategy options also:

  1. GenerationType.TABLE - the persistence provider uses a database table for managing keys.

  2. GenerationType.SEQUENCE - the persistence provider uses a databases sequence for key generation. The database must support Sequences

  3. GenerationType.IDENTITY - the persistence provider defers to the database for key generation. The database must support the IDENTITY column type.

Another point : They may have not provide such option because it will slow down the performance also. For each insert, it will have to search through whole ID column. You can imagine how much it will impact to performace.

Share:
21,983
Androider
Author by

Androider

Updated on September 03, 2020

Comments

  • Androider
    Androider over 3 years

    I am using this code in hibernate.

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="RightID", unique=true, nullable=false)
    

    The problem is when i delete a row the 'RightId' don't remain in sequence. I want something like, hibernates should check id, if some id value is missing it must give that value to 'RightsId' otherwise proceed normally