且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"正确"在HSQLDB 2.0.0-rc8中选择下一个序列值的方法

更新时间:2022-10-14 20:29:45


假设我有一个序列,称为TEST_SEQ什么是选择下一个值的正确方法是什么?


虽然 documentation 表示:


序列的下一个值可以包含在SELECT,INSERT和UPDATE语句如下例所示:

  SELECT [...,] NEXT VALUE FOR< sequencename> [,...] FROM< tablename>; 


正确的方式(因为更简单,因为不涉及表像HSQLDB不具备的DUAL DUAL表一样):

 调用[sequence_name]的下一个值; 

这出现在 1.7.2 ,这实际上Hibernate是如何处理最近的 HSQLDialect Hibernate Core版本(请参阅 HHH-2839 )。

事实上,这就是我在 HSQLDialect 中看到的 hibernate-core- 3.3.0.SP1.jar

  public String getSequenceNextValString(String sequenceName){
返回call next value for+ sequenceName;





$ b

所以我的建议是:升级到更新版本的Hibernate,你很可能使用Hibernate Core 3.2.5或更高版本。


suppose i have a sequence, called TEST_SEQ what would be the correct way of selecting its next value ? this does not work:

select next value for TEST_SEQ

probably because it expects a "FROM" clause. looking at HSQLDialect.getSequenceNextValString() in hibernate i see this:

"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):

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".

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 ?

suppose i have a sequence, called TEST_SEQ what would be the correct way of selecting its next value ?

While the documentation says:

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>;

the "correct" way (because simpler, because not involving a table like a dumb DUAL table that HSQLDB doesn't have) would be:

call NEXT VALUE FOR [sequence_name];

This appeared in 1.7.2 and this his actually how Hibernate handles sequences in the HSQLDialect of "recent" versions of Hibernate Core (see HHH-2839).

And indeed, this is what I see in the 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.