Autogenerated ids create gaps in sequence after em.remove() followed by an em.merge or em.persist
I was creating an EJB application which as a CRUD feature for Hotel Rooms. I have 4 rows in my Room database.
Now if i remove the 4th row (having room id 104) by using the em.remove() method and then add a new row the table looks like this
A gap is created in the autogenerated sequence Ids. Is there any way to avoid these gaps while adding new rows ?
By default JPA don't have this feature, also this issue is more related how to database manage the sequences and as far as I know sequence number are not reused.
Persist operation will create a new id, when the entity does not have one, if you try to persist a detached object probably you will find a EntityExistsException.
• If the Object is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).