简体   繁体   English

从表中删除记录如何使用Hibernate(HQL)为表中的已删除行重新生成ID

[英]Delete records from table how regenerate id for deleted row in the table using Hibernate(HQL)

I have a table. 我有桌子 A record from that table gets deleted (suppose id 1). 该表中的一条记录将被删除(假设ID为1)。 How can I Insert a new record with the same id using hibernate? 如何使用休眠方式插入具有相同ID的新记录? (right now I am using generated type auto ) (现在我正在使用生成的类型auto)

Is there any alternate solution for it instead of creating getter and setter for id? 是否有其他替代解决方案,而不是为id创建getter和setter?

Usually, in SGBD, you use a sequence that generates a new unique id on demand. 通常,在SGBD中,您使用按需生成新的唯一ID的序列。
When there is a delete, you generally don't care about reusing the freed slot. 删除后,您通常不必在意重新使用释放的插槽。 If you need to do it, it has to be with a custom development, by keeping a pool of available ids. 如果需要这样做,则必须通过保留可用 ID池进行自定义开发。 This means you need to recycle ids from the deleted entities and manage concurrency accesses to it in order to keep unicity. 这意味着您需要从已删除的实体中回收ID,并管理对其的并发访问,以保持唯一性。 This pool has to be persisted too. 该池也必须持久化。
All this has a cost, in complexity and performance. 所有这些都在复杂性和性能上付出了代价。

There are several resources if you want to implement a custom id generator, like this one , but you will need to implement the recycling of ids strategy which is certainly more complex and may be not easy to hook in hibernate when there is a delete. 如果您想实现一个自定义id生成器,则有多种资源,像这样的资源 ,但是您将需要实现ids回收策略,这肯定更复杂,并且在删除时可能不容易进入休眠状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM