gpt4 book ai didi

java - Hibernate 创建名称转义的表

转载 作者:行者123 更新时间:2023-11-29 07:14:11 25 4
gpt4 key购买 nike

我有一个如下所示的实体类:

@Entity
public class perm {

@Id
@GeneratedValue
private Long id;

private boolean add;

// Getter & Setter are also there ...
}

当我启动 spring(使用 JPA hibernate)时,他为我的 MySQL 数据库生成以下 CREATE TABLE 语句:

create table perm (id bigint not null auto_increment, add bit not null, primary key (id))

此创建将失败,因为列名 add 未使用

进行转义
`

就像平常应该的那样。
通常我会使用以下代码手动创建它:

create table perm (`id` bigint not null auto_increment, `add` bit not null, primary key (`id`))

当然,我可以创建一个ImprovedNamingStrategy,以便通过设置前缀或后缀来操作所有列名称。但是我的所有列都有这种语法。

现在我的问题:
是否有可能有一个 jpaProperty 在每次在 sql 语法中使用时都会转义列名?起初我以为,当我设置我的

jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");`

但显然事实并非如此。也许还有其他设置?

最佳答案

给字段添加@Column注解:

@Entity
public class perm {

@Id
@GeneratedValue
private Long id;
@Column(name="`add`")
private boolean add;

// Getter & Setter are also there ...
}

还可以考虑为该列使用更好的名称。这可能会在其他地方引起问题。

关于java - Hibernate 创建名称转义的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38550568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com