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

de90aj5v  于 2023-01-26  发布在  Spring
关注(0)|答案(9)|浏览(376)

我定义了以下实体:

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

尽管有表注解,我还是收到了java.sql.SQLException: Invalid object name 'email_template'。如何防止像EmailTemplate这样的实体类被转换为email_template表名?
编辑:
我正在使用Spring Boot:启动JPA。从我的build.gradle文件,

compile("org.springframework.boot:spring-boot-starter-data-jpa")
pu3pd22g

pu3pd22g1#

Spring默认使用org.springframework.boot.orm.jpa.SpringNamingStrategyorg.springframework.boot.orm.jpa.SpringNamingStrategy使用下划线分隔驼峰式名称。尝试在application.properties中设置spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy。查看thisthis以了解更多信息。

xpcnnkqh

xpcnnkqh2#

对于休眠版本5

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

ctzwtxfj3#

对于***Sping 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');
d6kp6zgx

d6kp6zgx4#

在您的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
xqnpmsa8

xqnpmsa85#

用途

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

qgzx9mmu6#

application.properties集合中

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

jm81lzqq7#

有两种最常见的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并且使用下划线...

ibrsph3r

ibrsph3r8#

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
bbmckpt7

bbmckpt79#

解决了。
无效的对象名称:使用JPA的Springboot(SQL服务器)
在应用程序. yaml/properties中指定
spring. jpa. hib.命名.隐式策略spring. jpa. hib.命名.物理策略
jpa:显示-SQL:假 hibernate :dldl-auto:none #未处于嵌入模式时默认为"none"命名:内隐策略:org. hib. boot. model. naming.隐式命名策略遗留Jpa实现物理策略:组织.休眠.引导.模型.命名.物理命名策略标准实施

相关问题