休眠自动递增

发布于 2024-09-12 15:56:28 字数 446 浏览 1 评论 0原文

我正在使用 hibernate 的“增量”生成器来生成我的自动增量 Id。这工作得很好,直到我不得不添加另一个应用程序,该应用程序也使用“增量”生成器来生成新的 id。我意识到增量生成器有一个问题:

"generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster."

有没有办法使用休眠来处理这个问题?或者我是否必须编写额外的代码来锁定表,检查数据库中的最大 Id(然后递增它)并最终释放锁?使用一个休眠配置不是一种选择。另外,我无法将 auto_increment 选项添加到 db 表,因为表上有多个键。

欢迎任何建议,我猜这是一个常见问题。

I am using hibernate's "increment" generator to generate my auto increment Id's. This was working fine until I had to add another application which also uses the "increment" generator to generate new id's. I realised there is a catch with the increment generator:

"generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster."

Is there any way to deal with this using hibernate? Or will I have to write additional code to lock the table, check the db for max Id (and then increment it) and finally release the lock? Using one hibernate configuration is not an option. Also I can not add the auto_increment option to the db table because we have more than one key on the table.

Any suggestions are welcome, I am guessing this is a common problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-09-19 15:56:28

还有其他几个生成器< /a> 这可能会满足您的需要:

  • hilo 使用 hi/lo 算法有效地生成 long、short 或 int 类型的标识符,给定表和列(默认情况下分别为 hibernate_unique_key 和 next_hi)作为 hi 价值观的源泉。 hi/lo 算法生成仅对于特定数据库唯一的标识符。
  • uuid:使用128位UUID算法生成在网络内唯一的字符串类型的标识符(使用IP地址)。 UUID 被编码为长度为 32 个十六进制数字的字符串。

我建议查看 hi/lo

此外,我无法将 auto_increment 选项添加到数据库表,因为表上有多个键。

我发现这个语句令人困惑 - 如果表的主键使用多个列,则不能让一列自动递增?

为什么你会有多个列的主键,其中一列无论如何都是唯一的 - 这使得 id 字段不可能有多个值,从而否定了复合PK。

There are several other generators which may give you what you need:

  • hilo uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.
  • uuid: uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.

I'd recommend taking a look at hi/lo.

Also I can not add the auto_increment option to the db table because we have more than one key on the table.

I find this statement confusing - you cannot have one column be auto-incrementing if the primary key of the table uses more than one column?

Why would you have a primary key of more than one column where one of the columns is unique anyway - this makes it impossible to ever have more than one value for the id field, thus negating the purpose of a compound PK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文