实体类名称转换为带下划线的 SQL 表名称

我界定了以下实体:

@Entity
@Table(name = "EmailTemplate")
public class EmailTemplate {

尽管有表注释,我还是收到了 java.sql.SQLException: Invalid object name 'email_template'。如何防止将 EmailTemplate 等实体类转换为 email _ template 表名?

编辑:

我使用的是 Spring Boot: start JPA,

compile("org.springframework.boot:spring-boot-starter-data-jpa")
77605 次浏览

Spring 默认使用 org.springframework.boot.orm.jpa.SpringNamingStrategy,它将驼峰大小写名称与下划线分开。尝试在 application.properties中设置 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy。查看 这个这个获得更多信息。

使用

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

对于 休眠 v5:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

解决了。

无效的对象名称: 带有 JPA 的 Springboot (SQL 服务器)

在 application.yaml/properties 中指定

隐式策略 物理策略

返回文章页面 Show-sql: false 冬眠: Ddl-auto: none # 在非嵌入模式下默认为“ none” 命名: 隐式策略: org.hibernate.boot.mod.name.ImpiciNamingStrategies yLegacyJpaImpl 物理-策略: org.hibernate.boot.mot.name.PhysicalNamingStrategies yStandardImpl

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a];
nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

两者都是必需的:

implicit-strategy
physical-strategy

application.properties设置

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

在你的 appplication.properties.中使用这个

spring.jpa.hibernate.naming.physical-strategy=
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl


spring.jpa.hibernate.naming.implicit-strategy=
org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

对于 Spring Boot 2 (用 2.2.6.RELEASE检查) ,它应该是配置 yml文件:

spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

所以你可以有这样的模型:

@Table(name = "tblDepartments")
public class Department {
@Id
@Column(name = "dpID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;


@NotEmpty
@Size(min = 1, max = 25)
@Column(name = "dpName", length = 25)
private String name;

并在启动时使用 data.sql填充表:

INSERT INTO tblDepartments (dpName) VALUES ('Gryffindor');
INSERT INTO tblDepartments (dpName) VALUES ('Hufflepuff');

有两种最常见的 org.hibernate.boot.model.naming.PhysicalNamingStrategy:

org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
# also deprecated in 2.6 in favor of CamelCaseToUnderscoresNamingStrategy
# for removal in 2.8
org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties持有:

private void applyNamingStrategies(Map<String, Object> properties) {
applyNamingStrategy(properties, AvailableSettings.IMPLICIT_NAMING_STRATEGY, this.implicitStrategy,
() -> SpringImplicitNamingStrategy.class.getName());
applyNamingStrategy(properties, AvailableSettings.PHYSICAL_NAMING_STRATEGY, this.physicalStrategy,
() -> CamelCaseToUnderscoresNamingStrategy.class.getName());
}

所以默认情况下 CamelCaseToUnderscoresNamingStrategy在使用中,并且你有下划线..。