Hibernate with Oracle 序列不使用它
我已将 hibernate 配置为使用 oracle 序列。序列是使用缓存 = 20、增量 = 1 创建的。
一切正常,休眠持久实体。 id值很奇怪:50,51....76,201,202...209,1008,1009,5129,5130 ....
如果我要求序列值(从dual中选择hibernate_sequence.nextval)我得到像2这样的值, 3,4 ....
如果我打开hibernate sql调试,有时会调用“select hibernate_sequence.nextval from Dual”,但 hibernate 分配给 ID 的数字并不按顺序传递!
@Id
@Column(name = "ID", insertable = false, updatable = false)
@SequenceGenerator(name = "SequenceIdGenerator", sequenceName = "HIBERNATE_SEQUENCE")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceIdGenerator")
private Long id;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 SequenceGenerator 并不是真正的序列生成器。这是一个序列高低发生器。这意味着第一次调用它时,它会从序列中获取下一个值(例如 6),然后将该值乘以 50,得到结果 (300)。下次调用它时,它返回 301(不转到序列),依此类推,直到达到 349。然后它向序列询问下一个值并获得 7,它再次乘以 50 得到 350。算法描述可能会有偏差,但你明白了。
如果您停止并启动应用程序,则会出现间隙。但它比纯序列生成器更高效,因为它每 50 代才调用一次数据库。
请参阅http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-id-enhanced-optimizers 和 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-id-generator 了解详细信息。
This is because the SequenceGenerator is not really a sequence generator. It's a sequence hi-lo generator. This means that the first time it's invoked, it gets the next value from the sequence (6 for example), then multiplies this value by 50 and gives you the result (300). The next time it's invoked, it returns 301 (without going to the sequence), and so on until it reaches 349. Then it asks the sequence for the next value and obtains 7, which it multiplies by 50 again to give you 350. My algorithm description could be off by one, but you get the idea.
If you stop and start your application, it will thus have gaps. But it's more efficient than a pure sequence generator because it only makes a database call once in 50 generations.
See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-id-enhanced-optimizers and http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-id-generator for details.
我认为您的问题是数据库中 ID 列的值不是自然序列,但为什么您会看到间隙:
一点背景知识:
select HIBERNATE_SEQUENCE.nextval from DUAL 序列的值增加。
例如,考虑以下场景:
您有两个实体 Entity1 和 Entity2,使用 HIBERNATE_SEQUENCE 作为 ID 生成器:
select HIBERNATE_SEQUENCE.nextval from DUAL
(返回 104)因此,最后您将得到:
这解释了这些差距。
编辑:
即使 @SequenceGenerator 设置为使用
SequenceGenerator
而不是SequenceHiLoGenerator
(正如 JB Nizet 所指出的,我认为这是对差距的更好解释) ,序列生成的 ID 中的缺口是常见的情况。I take it that your question is that the values of the ID column in the database are not a natural sequence, but why you are seeing gaps:
A bit of background:
select HIBERNATE_SEQUENCE.nextval from DUAL
the value of the sequence is increased.For example, consider the following scenario:
You've got two entities Entity1 and Entity2 using HIBERNATE_SEQUENCE as the ID generator:
select HIBERNATE_SEQUENCE.nextval from DUAL
(returns 104)So at the end of it you'll have:
which explains the gaps.
EDIT:
Even if the @SequenceGenerator were setup to use the
SequenceGenerator
rather than theSequenceHiLoGenerator
(as pointed out by JB Nizet, which I think is a better explanation for the gaps), gaps in IDs generated by sequences are a common occurrence.