Autogenerated ids create gaps in sequence after em.remove() followed by an em.merge or em.persist

StackOverflow https://stackoverflow.com/questions/21693598

  •  09-10-2022
  •  | 
  •  

質問

I was creating an EJB application which as a CRUD feature for Hotel Rooms. I have 4 rows in my Room database.

enter image description here

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

enter image description here

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).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top