【问题标题】:org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement in SpringBoot with h2 and JPAorg.hibernate.tool.schema.spi.CommandAcceptanceException:在使用 h2 和 JPA 的 SpringBoot 中通过 JDBC 语句执行 DDL 时出错
【发布时间】:2019-03-26 02:41:37
【问题描述】:

在使用 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]

这是由于以下原因引起的

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]

我的休眠类

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 在下面

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

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

【问题讨论】:

  • 缺少的部分是@column
  • 什么是FROM[*]

标签: spring hibernate spring-mvc spring-boot spring-data-jpa


【解决方案1】:

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

因此请尝试更改字段名称,否则请使用 name 属性和@Column Annotation(为数据库提供别名)

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

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

    private BigDecimal conversionMultiple;
    private int port;

【讨论】:

    【解决方案2】:

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

    ...

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

    ...

    【讨论】:

    • 感谢这工作。我喜欢堆栈溢出太多你忘记人们可以观察到的东西。
    • 发生在我身上以及使用“发布”
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2017-10-20
        • 2019-05-23
        • 1970-01-01
        • 2019-04-09
        • 2020-04-27
        • 2022-09-29
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        相关资源
        最近更新 更多