Java 在 HSQLDB 2.0.0-rc8 中选择下一个序列值的“正确”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2340037/
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
"correct" way to select next sequence value in HSQLDB 2.0.0-rc8
提问by radai
suppose i have a sequence, called TEST_SEQ what would be the correct way of selecting its next value ? this does not work:
假设我有一个名为 TEST_SEQ 的序列,选择其下一个值的正确方法是什么?这不起作用:
select next value for TEST_SEQ
probably because it expects a "FROM" clause. looking at HSQLDialect.getSequenceNextValString() in hibernate i see this:
可能是因为它需要一个“FROM”子句。在休眠中查看 HSQLDialect.getSequenceNextValString() 我看到了:
"select next value for " + sequenceName + " from dual_" + sequenceName
which in my case would result in something like:
在我的情况下,这会导致类似的结果:
select next value for TEST_SEQ from dual_TEST_SEQ
which does not work for 2.0.0-rc8 (i only assume this works in pre-2.0 versions - havent verified) I've come across a solution that involves creating a simple table with 1 row called DUAL, in which case this will work (oracle style):
这不适用于 2.0.0-rc8(我只假设这在 2.0 之前的版本中有效 - 尚未验证)我遇到了一个解决方案,该解决方案涉及创建一个名为 DUAL 的具有 1 行的简单表,在这种情况下这将起作用(甲骨文风格):
select next value for TEST_SEQ from DUAL
but hsqldb does not come with this table out of the box, and im not sure how i can get hibernate to generate such a table on "first boot".
但是 hsqldb 没有随附此表,我不确定如何让休眠在“首次启动”时生成这样的表。
Im thinking there has to be a way to get the next value for a sequence out of the box and im just missing it. any ideas ?
我认为必须有一种方法可以开箱即用地获取序列的下一个值,而我只是错过了它。有任何想法吗 ?
采纳答案by Pascal Thivent
suppose i have a sequence, called TEST_SEQ what would be the correct way of selecting its next value ?
假设我有一个名为 TEST_SEQ 的序列,选择其下一个值的正确方法是什么?
While the documentationsays:
虽然文档说:
The next value for a sequence can be included in SELECT, INSERT and UPDATE statements as in the following example:
SELECT [...,] NEXT VALUE FOR <sequencename> [, ...] FROM <tablename>;
序列的下一个值可以包含在 SELECT、INSERT 和 UPDATE 语句中,如下例所示:
SELECT [...,] NEXT VALUE FOR <sequencename> [, ...] FROM <tablename>;
the "correct" way (because simpler, because not involving a table like a dumb DUAL table that HSQLDB doesn't have) would be:
“正确”的方式(因为更简单,因为不涉及像 HSQLDB 没有的哑 DUAL 表之类的表)将是:
call NEXT VALUE FOR [sequence_name];
This appeared in 1.7.2and this his actually how Hibernate handles sequences in the HSQLDialect
of "recent" versions of Hibernate Core (see HHH-2839).
这出现在1.7.2 中,这实际上是 Hibernate 如何处理HSQLDialect
Hibernate Core“最新”版本中的序列(参见HHH-2839)。
And indeed, this is what I see in the HSQLDialect
of hibernate-core-3.3.0.SP1.jar
:
事实上,这就是我在HSQLDialect
of 中看到的hibernate-core-3.3.0.SP1.jar
:
public String getSequenceNextValString(String sequenceName) {
return "call next value for " + sequenceName;
}
So my advice is: upgrade to a newer version of Hibernate, you are very likely using Hibernate Core 3.2.5 or prior.
所以我的建议是:升级到更新版本的 Hibernate,你很可能使用 Hibernate Core 3.2.5 或更早版本。
回答by rogerdpack
Apparently if you run
显然,如果你跑
SET DATABASE SQL SYNTAX PGS
(PGS standing for Postgres)
SET DATABASE SQL SYNTAX PGS
(PGS 代表 Postgres)
then you can query it using standard Postgres syntax like select nextval('sequence_name')
http://hsqldb.org/doc/guide/dbproperties-chapt.html
然后你可以使用标准的 Postgres 语法来查询它,比如select nextval('sequence_name')
http://hsqldb.org/doc/guide/dbproperties-chapt.html
Also note that if you once do this , typical HSQLDB sequence like call NEXT VALUE FOR SEQUENCE_NAME
will no longer work.
另请注意,如果您一次这样做,典型的 HSQLDB 序列call NEXT VALUE FOR SEQUENCE_NAME
将不再起作用。