关于 H2(Oracle 模式)“语法错误:SELECT NEXTVAL FROM[*] DUAL”有什么想法吗?

发布于 2024-11-03 09:05:47 字数 2820 浏览 1 评论 0原文

出现以下错误

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:testdb;MODE=Oracle" />
        <property name="username" value="sa" />
        <property name="password" value="" />
</bean>

我在 Oracle 模式下使用 H2 (v1.3.154)和 Hibernate v3.5.6 的 Oracle 方言时

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

:错误:

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT 
SQ_PERSON_ID.NEXTVAL FROM[*] DUAL "; expected "identifier"; SQL statement:
select SQ_PERSON_ID.nextval from dual [42001-154]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.getSyntaxError(DbException.java:192)
at org.h2.command.Parser.readColumnIdentifier(Parser.java:2752)
at org.h2.command.Parser.readTermObjectDot(Parser.java:2311)
at org.h2.command.Parser.readTerm(Parser.java:2428)
at org.h2.command.Parser.readFactor(Parser.java:2025)
at org.h2.command.Parser.readSum(Parser.java:2012)
at org.h2.command.Parser.readConcat(Parser.java:1985)
at org.h2.command.Parser.readCondition(Parser.java:1850)
at org.h2.command.Parser.readAnd(Parser.java:1831)
at org.h2.command.Parser.readExpression(Parser.java:1823)
at org.h2.command.Parser.parseSelectSimpleSelectPart(Parser.java:1736)
at org.h2.command.Parser.parseSelectSimple(Parser.java:1768)
at org.h2.command.Parser.parseSelectSub(Parser.java:1663)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1508)
at org.h2.command.Parser.parseSelect(Parser.java:1496)
at org.h2.command.Parser.parsePrepared(Parser.java:401)
at org.h2.command.Parser.parse(Parser.java:275)
at org.h2.command.Parser.parse(Parser.java:247)
at org.h2.command.Parser.prepare(Parser.java:201)
at org.h2.command.Parser.prepareCommand(Parser.java:214)
at org.h2.engine.Session.prepareLocal(Session.java:426)
at org.h2.engine.Session.prepareCommand(Session.java:374)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1100)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:71)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:243)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:106)
... 81 more

知道会发生什么吗,我的配置中遗漏了?我找到了这篇文章并尝试了该补丁,但它无法解决该错误。感谢您的帮助 !

I have the following error using H2 (v1.3.154) with Oracle mode :

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:testdb;MODE=Oracle" />
        <property name="username" value="sa" />
        <property name="password" value="" />
</bean>

and Oracle dialect for hibernate v3.5.6 :

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

The error :

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT 
SQ_PERSON_ID.NEXTVAL FROM[*] DUAL "; expected "identifier"; SQL statement:
select SQ_PERSON_ID.nextval from dual [42001-154]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.getSyntaxError(DbException.java:192)
at org.h2.command.Parser.readColumnIdentifier(Parser.java:2752)
at org.h2.command.Parser.readTermObjectDot(Parser.java:2311)
at org.h2.command.Parser.readTerm(Parser.java:2428)
at org.h2.command.Parser.readFactor(Parser.java:2025)
at org.h2.command.Parser.readSum(Parser.java:2012)
at org.h2.command.Parser.readConcat(Parser.java:1985)
at org.h2.command.Parser.readCondition(Parser.java:1850)
at org.h2.command.Parser.readAnd(Parser.java:1831)
at org.h2.command.Parser.readExpression(Parser.java:1823)
at org.h2.command.Parser.parseSelectSimpleSelectPart(Parser.java:1736)
at org.h2.command.Parser.parseSelectSimple(Parser.java:1768)
at org.h2.command.Parser.parseSelectSub(Parser.java:1663)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1508)
at org.h2.command.Parser.parseSelect(Parser.java:1496)
at org.h2.command.Parser.parsePrepared(Parser.java:401)
at org.h2.command.Parser.parse(Parser.java:275)
at org.h2.command.Parser.parse(Parser.java:247)
at org.h2.command.Parser.prepare(Parser.java:201)
at org.h2.command.Parser.prepareCommand(Parser.java:214)
at org.h2.engine.Session.prepareLocal(Session.java:426)
at org.h2.engine.Session.prepareCommand(Session.java:374)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1100)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:71)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:243)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:106)
... 81 more

Any idea of what happens, a miss in my config ? I found this post and try the patch, it does not resolve the error. Thanks for your help !

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

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

发布评论

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

评论(6

高冷爸爸 2024-11-10 09:05:47

请确保已创建序列。如果它被创建,那么它对我有用:

create sequence SQ_PERSON_ID;
select SQ_PERSON_ID.nextval from dual;

如果它没有创建,那么就会抛出与您得到的相同的错误消息。

Please ensure the sequence is created. If it is created, then it works for me:

create sequence SQ_PERSON_ID;
select SQ_PERSON_ID.nextval from dual;

If it is not created, then the same error message is thrown as you got.

古镇旧梦 2024-11-10 09:05:47

我正在使用 Oracle 模式处理 h2,但上面提到的所有解决方案都不适合我。
尽管经过一些研究,我发现这个查询可以很好地获取序列中的下一个值。

选择 nextval('SchemaName', 'SequenceName');

I was working on h2 with Oracle mode but all the above solutions mentioned above didn't work for me.
Although after some research I found that this query will work fine for fetching the next value in the sequence.

select nextval('SchemaName', 'SequenceName');

流星番茄 2024-11-10 09:05:47

正如@longliveenduro 提到的

这绝对是表示“序列不存在”的 H2 方式。

我遇到了完全相同的问题,并发现该序列没有在 h2 的内存数据库中创建。我通过添加以下内容解决了问题:

CREATE SEQUENCE SQ_PERSON_ID
MINVALUE 1
MAXVALUE 9223372036854775807
START WITH 1
INCREMENT BY 1
CACHE 8
NOCYCLE;

在: test/resources/schema.sql

它对我有用。

As @longliveenduro mentioned

It's definitely the H2 way to say "Sequence does not exist".

I had the exact same problem and found out that the sequence was not being created in h2's in-memory DB. I solved the problem by adding:

CREATE SEQUENCE SQ_PERSON_ID
MINVALUE 1
MAXVALUE 9223372036854775807
START WITH 1
INCREMENT BY 1
CACHE 8
NOCYCLE;

in: test/resources/schema.sql

It worked for me.

莳間冲淡了誓言ζ 2024-11-10 09:05:47

检查您是否使用与创建序列相同的架构。如果没有,请在序列名称之前插入模式前缀,例如 MYUSER.MY_SEQ。

Check if you use the same schema under which the sequence is created. If not, insert a schema prefix before sequence name, such is MYUSER.MY_SEQ.

淡笑忘祈一世凡恋 2024-11-10 09:05:47

除了任何可能的 Hibernate 问题之外,您的 SA 用户是否具有针对序列的选择权限,该序列似乎处于不同的架构中? (请参阅文档中的示例)。

Aside from any possible Hibernate issue, does your SA user have select permission against the sequence, which seems to be in a different schema? (See example in documentation).

凉薄对峙 2024-11-10 09:05:47

再补充一点,h2 与 oracle 不同,是区分大小写的。所以DDL和java代码至少应该使用相同的序列名称大小写

Just to add one more point, h2 is case sensitive unlike oracle. so DDL and java code should at least use same case for sequence name

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