为什么hibernate会导致sql语法错误,尽管配置了正确的方言?

qybjjes1  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(385)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

4个月前关门了。
改进这个问题
我被难住了。我正在建立一个新的spring boot项目。我已经创建了几个实体类。它能够在数据库中创建一半的表,所以我知道大部分是能够连接到数据库的。我已经检查过了,并且在配置中设置了方言-我正在使用org.hibernate.dialect.mysqldialect。当我试着运行这个问题时,我总是会遇到这样一组错误:

Caused by: java.sql.SQLSyntaxErrorException: 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 'integer, symbol varchar(5), primary key (id)) engine=InnoDB' at line 1

我试着检查谷歌和其他stackoverflow问题,但没有结果。主要是我的发现引导我确定我有正确的方言设置,并避免保留关键字,我检查。
下面是显示的其他两个错误:

Caused by: java.sql.SQLSyntaxErrorException: 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 'integer, primary key (id)) type=MyISAM' at line 1
Caused by: java.sql.SQLSyntaxErrorException: 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 'varchar(255), clock_in varchar(255), clock_out varchar(255), coin varchar(255), ' at line 1

编辑:下面是hibernate生成的sql:

Hibernate: create table record (id integer not null, cash varchar(255), check varchar(255), clock_in varchar(255), clock_out varchar(255), coin varchar(255), primary key (id)) type=MyISAM
create table route (id integer not null, name varchar(100), color integer, driver varchar(255), index integer, primary key (id)) type=MyISAM
create table store (id integer not null, name varchar(100), address varchar(255), index integer, symbol varchar(255), primary key (id)) type=MyISAM

编辑2-以下是一个似乎在自动生成表时遇到问题的实体类:

package org.tampasa.kettles.models;

import javax.persistence.Entity;

@Entity
public class Store extends AbstractEntity{

    private String address;

    private String symbol;

    private int index;

    // Constructors

    public Store() {
    }

    // Getters and Setters

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

}

这里是抽象实体:

package org.tampasa.kettles.models;

import org.hibernate.validator.constraints.Length;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotBlank;
import java.util.Objects;

@MappedSuperclass
public abstract class AbstractEntity {

    @Id
    @GeneratedValue
    private int id;

    @NotBlank(message = "Name must not be blank")
    @Length(min = 1, max = 100, message = "Name must be between 1 and 100 characters")
    private String name;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AbstractEntity that = (AbstractEntity) o;
        return id == that.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

}
daolsyd0

daolsyd01#

一个可能的原因是 name 以及 index 使用sql保留字。至少在mysql 8.0中。一般来说也是。我也偶然发现了d
根据这个答案你可以加一行 hibernate.globally_quoted_identifiers=true 给你的 .properties 文件。这将使hibernate避开保留字,也许可以解决您遇到的问题。

相关问题