hibernate @GeneratedValue , how to deal with deleted rows. in mysql

流过昼夜 提交于 2021-02-07 10:25:47

问题


@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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!