休眠自动递增
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还有其他几个生成器< /a> 这可能会满足您的需要:
我建议查看 hi/lo。
我发现这个语句令人困惑 - 如果表的主键使用多个列,则不能让一列自动递增?
为什么你会有多个列的主键,其中一列无论如何都是唯一的 - 这使得
id
字段不可能有多个值,从而否定了复合PK。There are several other generators which may give you what you need:
I'd recommend taking a look at hi/lo.
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.