使用Hibernate和H2创建表时出现错误。

huangapple go评论83阅读模式
英文:

Table creation error using Hibernate and H2

问题

我正在尝试运行一个使用H2内存数据库和Hibernate的测试。但在上下文启动期间,我遇到以下错误:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: 在SQL语句中的语法错误 "CREATE TABLE ANIMAL (ID BIGINT NOT NULL, CREATIONDATE DATETIME, CREATOR VARCHAR(255), LASTMODIFICATION DATETIME, LASTMODIFIEDBY VARCHAR(255), VERSION BIGINT, ALLERGIES VARCHAR(255), ANAMNESIS VARCHAR(255), BREED VARCHAR(255), COMMENT VARCHAR(255), DATEOFBIRTH DATETIME, DECEASED BIT NOT NULL, DIET VARCHAR(255), HUSBANDRY VARCHAR(255), IDCODE VARCHAR(255), NAME VARCHAR(255), SEX INTEGER, WEIGHT FLOAT, FK_OWNER BIGINT, TAXONOMY_ID BIGINT, PRIMARY KEY (ID)) ENGINE=[*]MYISAM"; 预期 "identifier";

我做错了什么?
这是application.properties文件:

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create

相同的错误在Hibernate尝试创建所有数据库时都在重复出现:

org.h2.jdbc.JdbcSQLSyntaxErrorException: 在SQL语句中的语法错误 "CREATE TABLE HIBERNATE_SEQUENCE (NEXT_VAL BIGINT) ENGINE=[*]MYISAM"; 预期 "identifier";
英文:

I'm trying to run a test that uses H2 in-memory database and hibernate.
But I get the following error during context start-up:

> Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in
> SQL statement "CREATE TABLE ANIMAL (ID BIGINT NOT NULL, CREATIONDATE
> DATETIME, CREATOR VARCHAR(255), LASTMODIFICATION DATETIME,
> LASTMODIFIEDBY VARCHAR(255), VERSION BIGINT, ALLERGIES VARCHAR(255),
> ANAMNESIS VARCHAR(255), BREED VARCHAR(255), COMMENT VARCHAR(255),
> DATEOFBIRTH DATETIME, DECEASED BIT NOT NULL, DIET VARCHAR(255),
> HUSBANDRY VARCHAR(255), IDCODE VARCHAR(255), NAME VARCHAR(255), SEX
> INTEGER, WEIGHT FLOAT, FK_OWNER BIGINT, TAXONOMY_ID BIGINT, PRIMARY
> KEY (ID)) ENGINE=[*]MYISAM"; expected "identifier";

What am I doing wrong?
Here is application.properties file:

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create

The same error is repeating for all databases that hibernate tries to create:

> org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement
> "CREATE TABLE HIBERNATE_SEQUENCE (NEXT_VAL BIGINT) ENGINE=[*]MYISAM";
> expected "identifier"

答案1

得分: 1

问题最终出在配置文件中,我在其中有错误的代码行:

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    
    return properties;
}

将其修改为:

properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));

然后一切都开始正常工作了。

英文:

So at the end issue was in configuration file where I had wrong lines:

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    return properties;
}

changed it with:

properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));

And all started working fine

huangapple
  • 本文由 发表于 2020年10月14日 19:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64352012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定