org.hibernate.tool.schema.spi.CommandAcceptanceException尝试连接MYSQL数据库

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

org.hibernate.tool.schema.spi.CommandAcceptanceException trying to connect a MYSQL database

问题

I'm trying to connect a MySQL database to my Java Spring Boot project, but I got an org.hibernate.tool.schema.spi.CommandAcceptanceException error. I've already tried changing spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update, but it's still not working.

错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table to_do (id integer not null, desc varchar(255), done bit not null, target_date date, username varchar(255), primary key (id)) engine=InnoDB" via JDBC [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(255), done bit not null, target_date date, username varchar(255), p' at line 1]

属性:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework=info
logging.level.com.pao.springboot.myFirstWebApp=debug
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/todos
spring.datasource.username=todos-user
spring.datasource.password=dummytodos
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>jakarta.servlet.jsp.jstl</groupId>
        <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>glassfish-jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>5.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap-datepicker</artifactId>
        <version>1.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

实体类:

package com.pao.springboot.myFirstWebApp.toDo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

import java.time.LocalDate;

// 数据库
// 待办事项列表

@Entity(name = "ToDo")
public class ToDo {

    @Id
    @GeneratedValue
    private int id;
    private String username;
    private String desc;
    private LocalDate targetDate;
    private boolean done;

    public ToDo() {}

    public ToDo(int id, String username, String desc, LocalDate targetDate, boolean done) {
        this.id = id;
        this.username = username;
        this.desc = desc;
        this.targetDate = targetDate;
        this.done = done;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public LocalDate getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(LocalDate targetDate) {
        this.targetDate = targetDate;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    @Override
    public String toString() {
        return "ToDo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", desc='" + desc + '\'' +
                ", targetDate=" + targetDate +
                ", done=" + done +
                '}';
    }
}

希望这可以帮助你,谢谢阅读。我已经尝试将 spring.jpa.hibernate.ddl-auto 更改为 spring.jpa.hibernate.ddl-auto=update

英文:

Im trying to connect a MySQL database to my java springboot project, but i got a org.hibernate.tool.schema.spi.CommandAcceptanceException error, i already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update, and its still wrong.

Error:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;create table to_do (id integer not null, desc varchar(255), done bit not null, target_date date, username varchar(255), primary key (id)) engine=InnoDB&quot; via JDBC [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;desc varchar(255), done bit not null, target_date date, username varchar(255), p&#39; at line 1]

Properties:

spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp logging.level.org.springframework=info logging.level.com.pao.springboot.myFirstWebApp=debug
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/todos spring.datasource.username=todos-user spring.datasource.password=dummytodos spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Dependencies:

&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
	&lt;!--&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;/dependency&gt;--&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;version&gt;8.0.13&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.servlet.jsp.jstl&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.servlet.jsp.jstl-api&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
&lt;artifactId&gt;glassfish-jstl&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;bootstrap&lt;/artifactId&gt;
&lt;version&gt;5.1.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;bootstrap-datepicker&lt;/artifactId&gt;
&lt;version&gt;1.9.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;jquery&lt;/artifactId&gt;
&lt;version&gt;3.6.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;`

Entity:

ackage com.pao.springboot.myFirstWebApp.toDo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.time.LocalDate;
//Database
// List of todos
@Entity(name = &quot;ToDo&quot;)
public class toDo {
@Id
@GeneratedValue
private int id;
private String username;
private String desc;
private LocalDate targetDate;
private boolean done;
public toDo(){}
public toDo(int id, String username, String desc, LocalDate targetDate, boolean done) {
this.id = id;
this.username = username;
this.desc = desc;
this.targetDate = targetDate;
this.done = done;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public LocalDate getTargetDate() {
return targetDate;
}
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
@Override
public String toString() {
return &quot;toDo{&quot; +
&quot;id=&quot; + id +
&quot;, user=&#39;&quot; + username + &#39;\&#39;&#39; +
&quot;, desc=&#39;&quot; + desc + &#39;\&#39;&#39; +
&quot;, targetDate=&quot; + targetDate +
&quot;, done=&quot; + done +
&#39;}&#39;;
}
}

I hope someone can help me, thanks to read me.

I already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update.

答案1

得分: 1

@Column(name = "description")
private String desc;
import jakarta.persistence.Column;
英文:

The cause is simple - DESC is a reserved keyword in MySQL (and in SQL in general), used to specify descending order, so you may want to name your column explicitly:

@Column(name = &quot;description&quot;)
private String desc;

UPD:
please don't forget to import corrent @Column:

import jakarta.persistence.Column;

huangapple
  • 本文由 发表于 2023年6月9日 02:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434679.html
匿名

发表评论

匿名网友

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

确定