休眠生成器增量句柄如何删除 [英] how does hibernate generator increment handle deletes
问题描述
这就是我使用发电机的方式:
This is how i am using the generator:
<id name="id" column="column_name">
<generator class="increment"/>
</id>
private Integer id;
我看到的行为是:
- 创建第一个对象
- 休眠分配id = 1
- 删除该对象
- 关闭服务器并重新启动它(答案后添加了此内容)
- 创建第二个对象
- 休眠分配id = 1
注意:我希望新的数字是2,即使1不再存在.
note: I expected the new number to be 2, even though 1 doesnt exist anymore.
我仅使用HSQLDB对此进行了测试.
I have only tested this using HSQLDB.
问题:这是预期的行为吗?
Question: Is this expected behaviour?
AFAICT in the source, the next number should be 2 https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/id/IncrementGenerator.java#L68
更新:根据Ralph的回答,如果我跳过第4步(不要关闭服务器),则它会正确递增.这是因为在启动时将该值设置为max(id),然后将其存储在内存中.
Update: As per Ralph's answer, if I skip step 4 (dont shut down the server), it increments correctly. Its because the value gets set to max(id) on start up, and stored in memory after that.
推荐答案
IncrementGenerator
用"select max(" + column + ") from " + buf.toString();
初始化,其中column
是ID列.
The IncrementGenerator
is initialized with "select max(" + column + ") from " + buf.toString();
where column
is the ID column.
这意味着每次创建新的IncrementGenerator时(更精确:在调用configure
之后第一次调用generate
),id计数器将从数据库中重置"为最大值.
This means every time a new IncrementGenerator is created (more precise: the first time generate
is invoked after configure
is invoked) the id counter will be "reseted" to the max value from the DB.
这篇关于休眠生成器增量句柄如何删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!