休眠@GeneratedValue,如何处理已删除的行.在MySQL中 [英] hibernate @GeneratedValue , how to deal with deleted rows. in mysql

查看:46
本文介绍了休眠@GeneratedValue,如何处理已删除的行.在MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int color_id; 

上面的代码生成一个自动递增的数字,例如1,2,3,4,5,6.但是,当我们删除带有6的记录时,下一个生成的数字是7.但是我希望下一个生成的数字是6.我该怎么办?我需要自动递增才能从6,7本身继续而不是7,8有什么办法吗?

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?

推荐答案

自动生成的保证只是密钥的唯一性.这就是您所需要的.甚至在某些情况下,自动生成也可以提供一堆密钥来提高共享数据库的效率:

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 :

  • 客户要求创建新行
  • 为该客户端连接保留连续的ID系列
  • 此客户端可以创建许多新行,而无需为新id重新询问数据库

这种情况几乎总是会生成行ID,

This case will almost always generate row id that

  • 包含孔
  • id插入后不一定会增加

相信我:一些生产系统对此感到满意,因为它减少了对网络和数据库的争用.

Trust me : some production systems are glad with it because it causes less contention on network and database.

如果您要严格无间断的连续id,但不要要求自动数据库id生成来为您完成操作.

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.

这篇关于休眠@GeneratedValue,如何处理已删除的行.在MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆