问题
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int color_id;
the above generates a automatic incremented number like 1,2,3,4,5,6. But when we delete record with 6 , the next generated number is 7. but i want next generated number to be 6. what should i do? i need auto increment to continue from 6,7 itself not 7,8 is there any way?
回答1:
What autogeneration guarantee is only unicity of the key. And that's all you should need. There are even cases where autogeneration gives a bunch of keys to improve efficiency with shared databases :
- a client asks to create a new row
- a consecutive serie of id is reserved for that client connection
- this client may create many new lines without re-asking database for new ids
This case will almost always generate row id that
- contains hole
- id are not necessarily incremented following time of insertion
Trust me : some production systems are glad with it because it causes less contention on network and database.
You are on your own if you want strictly consecutive id with no hole, but do not ask automatic database id generation to do it for you.
来源:https://stackoverflow.com/questions/26651754/hibernate-generatedvalue-how-to-deal-with-deleted-rows-in-mysql