JPA,重复使用已删除的ID [英] JPA, Reuse of deleted ID

查看:51
本文介绍了JPA,重复使用已删除的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有生成ID的实体(因此,实体具有一个用@Id@GeneratedValue标记的字段).我将在业务逻辑中使用实体的生成ID,并且有一个问题:

I use an Entity with generated ID (so Entity has a field, that marked by @Id and @GeneratedValue). I'm going to use generated id of the entities in the business logic and I have a question:

如果我删除一个实体,它的ID是否可以重复使用? IE.新生成的ID只能比以前生成的ID大吗?

If I delete an entity, its ID will be re-used or not? I.e. new generated ID can be only bigger that all generated before?

推荐答案

您不需要重复使用ID,也不必浪费时间为此担心. (这是初学者的数据库问题.)

You don't need to reuse IDs, and shouldn't need to waste time worrying about this. (This is a beginner's database question.)

通常,从技术上讲,重用ID是可能的-尽管不方便-但它需要锁定整个表,因此对于并发/多用户使用效率极低.

In general, reusing IDs would technically be possible -- though inconvenient -- but it requires locking the entire table and hence is extremely inefficient for concurrent/ multiuser use.

此外,恢复的ID不会是连续的范围,因此表示效率很低.

Also, recovered IDs would not be a contiguous range.. and thus inefficient to represent.

没有认真的数据库系统实现这种方案.

No serious database system implements such a scheme.

所以,不用担心恢复.只要确保您分配的键类型(整数或长型)具有足够的容量(对于INTEGER为20亿,对于long/BIGINT为2 ^ 63),就足以在合理的最大交易量下使用200多年.

So, don't worry about recovery. Just ensure your allocated key-type (integer or long) has enough capacity (2 billion for INTEGER, 2^63 for long/BIGINT) that it will suffice for 200+ years of use at the largest reasonably possible transaction volumes.

例如,当前每天有10,000条记录:

For example, at a current 10,000 records per day volume:

  • 每年365万
  • 业务增长的20倍=每年7300万
  • 时间200年= 146亿

这只有〜34位-对于'int'来说太大了,但是使用长(63位为正数),则可以为您提供2 ^ 29(〜5亿)倍的备用容量.

This is only ~34 bits -- too big for 'int', but using a long (63 bits positive) it gives you spare capacity by a factor of 2^29 (~500 million) times.

然后您可以告诉您的老板,没有必要进行密钥恢复,并且有把握地预测该系统在未来1000亿年内不会在任何时间用完密钥.

You can then tell your boss that key recovery is not necessary, and the system is confidently projected not to run out of keys anytime in the next 100 billion years.

这篇关于JPA,重复使用已删除的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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