实体 Class 名称转换为 SQL table 带下划线的名称
Entity Class name is transformed into SQL table name with underscores
我定义了以下实体:
@Entity
@Table(name = "EmailTemplate")
public class EmailTemplate {
尽管有 table 注释,我还是收到了 java.sql.SQLException: Invalid object name 'email_template'
。如何防止实体 class(例如 EmailTemplate)被转换为 email_template table 名称?
编辑:
我正在使用 Spring 引导:启动 JPA。从我的 build.gradle 文件中,
compile("org.springframework.boot:spring-boot-starter-data-jpa")
Spring 默认使用 org.springframework.boot.orm.jpa.SpringNamingStrategy
将驼峰命名法与下划线分开。尝试在 application.properties
中设置 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy
。查看 this and this 了解更多信息。
使用
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
对于 hibernate v5:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
已解决。
无效的对象名称:带 JPA 的 Springboot(SQL 服务器)
在申请中。yaml/properties指定
spring.jpa.hibernate.naming.implicit-策略
spring.jpa.hibernate.naming.physical-策略
日本帕:
显示-sql: false
休眠:
ddl-auto: none # 非嵌入式模式时默认为 "none"
命名:
隐式策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
物理策略:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
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
正在使用并且您有下划线...
我定义了以下实体:
@Entity
@Table(name = "EmailTemplate")
public class EmailTemplate {
尽管有 table 注释,我还是收到了 java.sql.SQLException: Invalid object name 'email_template'
。如何防止实体 class(例如 EmailTemplate)被转换为 email_template table 名称?
编辑:
我正在使用 Spring 引导:启动 JPA。从我的 build.gradle 文件中,
compile("org.springframework.boot:spring-boot-starter-data-jpa")
Spring 默认使用 org.springframework.boot.orm.jpa.SpringNamingStrategy
将驼峰命名法与下划线分开。尝试在 application.properties
中设置 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy
。查看 this and this 了解更多信息。
使用
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
对于 hibernate v5:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
已解决。
无效的对象名称:带 JPA 的 Springboot(SQL 服务器)
在申请中。yaml/properties指定
spring.jpa.hibernate.naming.implicit-策略 spring.jpa.hibernate.naming.physical-策略
日本帕: 显示-sql: false 休眠: ddl-auto: none # 非嵌入式模式时默认为 "none" 命名: 隐式策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl 物理策略:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
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
正在使用并且您有下划线...