spring org.hibernate.tool.schema.spi.CommandAcceptanceException:使用 h2 和 JPA 在 SpringBoot 中通过 JDBC 语句执行 DDL 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52922292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:39:39  来源:igfitidea点击:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement in SpringBoot with h2 and JPA

springhibernatespring-mvcspring-bootspring-data-jpa

提问by shubh

While running spring boot with h2 database and JPA i am getting below error.

在使用 h2 数据库和 JPA 运行 spring boot 时,我遇到了以下错误。

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]

It is caused due to below one

这是由于以下原因造成的

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE EXCHANGE_VALUE (ID INTEGER NOT NULL, CONVERSION_MULTIPLE DECIMAL(19,2), FROM[*] VARCHAR(255), PORT INTEGER NOT NULL, TO VARCHAR(255), PRIMARY KEY (ID)) "; expected "identifier"; SQL statement:
create table exchange_value (id integer not null, conversion_multiple decimal(19,2), from varchar(255), port integer not null, to varchar(255), primary key (id)) [42001-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.message.DbException.getSyntaxError(DbException.java:217) ~[h2-1.4.197.jar:1.4.197]

My hibernate class

我的冬眠课

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Exchange_Value")
public class ExchangeValue {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id; 
    private String from;
    private String to;
    private BigDecimal conversionMultiple;
    private int port;

    public ExchangeValue() {

    }

    public ExchangeValue(String from, String to, BigDecimal conversionMultiple) {
        super();
//      this.id = id;
        this.from = from;
        this.to = to;
        this.conversionMultiple = conversionMultiple;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }   
}

application.properties is below

application.properties 在下面

spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.hibernate.ddl-auto= create-drop

Just want to know as to what i am missing in the code tried adding spring.jpa.hibernate.ddl-auto= create-drop but it did not helped.

只是想知道我在代码中缺少什么尝试添加 spring.jpa.hibernate.ddl-auto= create-drop 但它没有帮助。

回答by ganesh045

@shubh.. Your Entity Field names are matching with SQL reserved keywords,

@shubh .. 您的实体字段名称与SQL reserved keywords,

So try to change the field names otherwise use nameattribute with @Column Annotation(which gives alias names to the DATABASE)

所以尝试更改字段名称,否则使用name属性 with @Column Annotation(它为数据库提供别名)

    @Column(name="valueFrom") 
    private String from;

    @Column(name="valueTo") 
    private String to;

    private BigDecimal conversionMultiple;
    private int port;

回答by Khairul Bashar Lemon

Your Entity Field name from was matched with database reserved word from, change the field name to another, or add a @Column annotation on that field. Like:

您的实体字段名称 from 与数据库保留字 from 匹配,将字段名称更改为其他名称,或在该字段上添加 @Column 注释。喜欢:

...

...

@Column(name = "_from")
private String from;

...

...

回答by S Debasish Nayak

I faced the same issue. I did the mistake in giving the schema name in mysql database.

我遇到了同样的问题。我在 mysql 数据库中给出模式名称时犯了错误。

In spring.properties -> (spring boot application)
spring.datasource.url=jdbc:mysql://localhost:3306/db_microservice

Here instead of "db_microservice" I Have given the name as "db-microservice". So don't use "-". And this solved my issue.

在这里,我将名称命名为“db-microservice”,而不是“db_microservice”。所以不要使用“-”。这解决了我的问题。

回答by Subbre

@ganesh045's response isnt actually true because hibernate's table creation query works like this: CREATE TABLE <your_table> 'password'for example to each of your attributes. Adding the `` to the attribute will make SQL read it NOTas reserved keyword. It also does the same when it queries for your select clauses inside JpaRepository. It will make a call such as this: SELECT 'attribute' FROM <your_table>. Your problem is most likely that you specified your schema name as kebab-case or anything which is not camelCase and snake_case. Also, with this approach you can have a table called User with attribute's usernameand passwordwhich actually also are MYSQL reserved keywords.

@ganesh045 的响应实际上并不正确,因为 hibernate 的表创建查询是这样工作的:CREATE TABLE <your_table> 'password'例如对您的每个属性。将`` 添加到属性将使SQL 将其NOT作为保留关键字读取。当它在 JpaRepository 中查询您的 select 子句时,它也会做同样的事情。它将使一个调用,如这样:SELECT 'attribute' FROM <your_table>。您的问题很可能是您将架构名称指定为 kebab-case 或任何不是camelCase和snake_case的名称。此外,使用这种方法,你可以有属性的表称为用户usernamepassword这实际上也是MYSQL reserved keywords