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

时间:2015-03-16 21:52:24

标签: spring-boot jpa spring-data-jpa

我定义了以下实体:

@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")

8 个答案:

答案 0 :(得分:44)

Spring默认使用org.springframework.boot.orm.jpa.SpringNamingStrategy,它使用下划线分割驼峰案例名称。尝试在spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy中设置application.properties。查看thisthis了解详情。

答案 1 :(得分:33)

hibernate v5

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

答案 2 :(得分:3)

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

答案 3 :(得分:2)

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

答案 4 :(得分:1)

使用

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

答案 5 :(得分:0)

application.properties组中

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

答案 6 :(得分:-1)

已解决。

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

在application.yaml / properties中指定

spring.jpa.hibernate.naming.implicit-strategy spring.jpa.hibernate.naming.physical-strategy

jpa:     show-sql:错误     休眠:       ddl-auto:none#不处于嵌入式模式时默认为“ none”       命名:         隐式策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl         物理策略:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

答案 7 :(得分:-1)

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