如何解决尽管配置了正确的方言,为什么Hibernate仍会导致SQL语法错误?
我很困惑。我正在建立一个新的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
我尝试检查google和其他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: create table record (id integer not null,cash varchar(255),check 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,address varchar(255),symbol varchar(255),primary key (id)) type=MyISAM
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;
}
}
这是AbstractEntity:
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);
}
}
解决方法
一个可能的原因可能是列name
和index
使用SQL保留字。至少在MySQL 8.0中。而且通常也是。
我偶然发现了too。 :D
根据this answer,您可以将hibernate.globally_quoted_identifiers=true
行添加到.properties
文件中。这应该使Hibernate转义保留字,并且也许可以解决您遇到的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。