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
I don't think there is any such optio available in hibernate. Instead of AUTO,you can try the following strategy options also:
GenerationType.TABLE - the persistence provider uses a database table for managing keys.
GenerationType.SEQUENCE - the persistence provider uses a databases sequence for key generation. The database must support Sequences
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With