java Hsqldb/H2中单元测试的SequenceGenerator问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7473131/
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
SequenceGenerator problem with unit testing in Hsqldb/H2
提问by RandomQuestion
I have tried using both Hsqldb and H2 for unit testing but facing problem with sequence generators. Field declaration looks like this.
我曾尝试使用 Hsqldb 和 H2 进行单元测试,但面临序列生成器的问题。字段声明如下所示。
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="keyword_seq")
@SequenceGenerator(name="keyword_seq",sequenceName="KEYWORD_ID_SEQ", allocationSize=1)
@Column(name="IM_KEYWORD_ID")
private Long keywordId;
When I try to test simple insert in this table in Hsqldb with following configuration, it gives error
当我尝试使用以下配置在 Hsqldb 中测试此表中的简单插入时,出现错误
<prop key="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:mem:testdb;sql.syntax_ora=true</prop>
Error :
错误 :
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: KEYWORD_ID_SEQ.NEXTVAL
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
With H2:
与 H2:
<prop key="hibernate.connection.driver_class">org.h2.Driver</prop>
<prop key="hibernate.connection.url">jdbc:h2:~/test</prop>
Error is :
错误是:
Hibernate: select KEYWORD_ID_SEQ.nextval from dual
3085 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 42001, SQLState: 42001
3088 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Syntax error in SQL statement "SELECT KEYWORD_ID_SEQ.NEXTVAL FROM[*] DUAL "; expected "identifier"; SQL statement:
Any idea, how to solve this?
任何想法,如何解决这个问题?
回答by gkamal
I think the dialect is set wrongly. Can you check your hibernate configuration.
我认为方言设置错误。你能检查一下你的休眠配置吗?