oracle 休眠增量起始编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1296826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:49:22  来源:igfitidea点击:

Hibernate increment starting number

javaoraclehibernatederby

提问by KevMo

I'm using hibernate to persist an entity to my database. For the moment it's a derby DB, but I will be moving it over to an oracle DB soon.

我正在使用 hibernate 将实体持久化到我的数据库中。目前它是一个 derby DB,但我很快就会将它转移到一个 oracle DB。

in my classname.hbm.xml I have the id defined as such:

在我的 classname.hbm.xml 中,我的 id 定义如下:

<id name="id" type="long">
      <column name="ID"/>
      <generator class="increment"/>
</id>

How do I set the starting value for the id? I'd like it to start at something like 10000 rather than 1.

如何设置 id 的起始值?我希望它从 10000 而不是 1 开始。

回答by skaffman

"increment" isn't really a good idea for ID generation.

“增量”对于 ID 生成来说并不是一个好主意。

If you're moving to Oracle, then you'll use a sequence to generate IDs, and the sequence is controlled by oracle, so you can make it start with whichever value you want.

如果您要迁移到 Oracle,那么您将使用一个序列来生成 ID,并且该序列由 oracle 控制,因此您可以让它以您想要的任何值开始。

Derby support sequencesas of 10.6, though you need Hibernate 3.6 to get it to work..

从 10.6 开始,Derby 支持序列,但您需要 Hibernate 3.6 才能使其工作..

回答by Jon

I don't think you can, it appears that IncrementGenerator works based off the highest primary key so unless you fix your primary keys (which I wouldn't recommend) you're stuck with that functionality.

我认为您不能,似乎 IncrementGenerator 基于最高主键工作,因此除非您修复主键(我不推荐),否则您会坚持使用该功能。

Would recommend a different strategy, it isn't safe in a cluster of machines either... Look at the Hibernate docs and maybe the enhanced identifier generators:

会推荐一种不同的策略,它在机器集群中也不安全......查看 Hibernate 文档,也许还有增强的标识符生成器:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-id-enhanced

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-id-enhanced

回答by Ruben

Generator type incrementlooks up the maximum numeric primary key value in the table and increments it by one.

生成器类型增量在表中查找最大的数字主键值并加一。

Generator type nativeuses a sequence when the underlying database used is Oracle. You can easily augment the value of the sequence to an arbitrary start value.

当使用的底层数据库是 Oracle 时,生成器类型native使用序列。您可以轻松地将序列的值增加到任意起始值。