【发布时间】:2021-01-23 21:53:01
【问题描述】:
我正在使用 Spring Boot 2.3.4 / Spring Data / Hibernate / MySQL 8。
我的数据库表名为“productDef”。我有一个实体对象“ProductDef”。
当我执行“列出所有”查询时,我收到此错误...
java.sql.SQLSyntaxErrorException: Table 'mydatabase.product_def' doesn't exist
Hibernate 为什么要查找表“product_def”?
我尝试向实体添加注释 @Table(name="productDef"),但这没有帮助。
如果我将 db 表重命名为“product_def”,我的代码就可以工作。但不幸的是,我无法为我的项目重命名数据库表名。
我错过了什么?
更新:
解决方案是实施自定义 PhysicalNamingStrategy 以防止“productDef”变为“product_def”。
但是,虽然这适用于 @Table 名称注释,但不适用于 @Column 名称注释。
根据to this discussion thread,忽略@Column名称注解是个bug。
将此添加到 application.properties:
spring.jpa.properties.hibernate.physical_naming_strategy=com.myapp.dao.RealNamingStrategyImpl
并像这样实现类:
package com.myapp.dao;
import java.io.Serializable;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class RealNamingStrategyImpl extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
public static final PhysicalNamingStrategy INSTANCE = new PhysicalNamingStrategyStandardImpl();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText(), name.isQuoted());
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText(), name.isQuoted());
}
}
【问题讨论】:
标签: mysql spring spring-boot hibernate spring-data