【问题标题】:Entity Class name is transformed into SQL table name with underscores实体类名转化为带下划线的SQL表名
【发布时间】:2015-05-19 04:55:00
【问题描述】:

我定义了以下实体:

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

【问题讨论】:

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


    【解决方案1】:

    Spring 默认使用org.springframework.boot.orm.jpa.SpringNamingStrategy,它用下划线分割骆驼大小写名称。尝试在application.properties 中设置spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy。查看thisthis 了解更多信息。

    【讨论】:

    • 对于休眠 v5:spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    • 这可以通过注释而不是属性文件来完成吗?
    • 我们如何在 application.yml 文件中实现这一点??
    • @SyedZeeshanAli:它应该和你对服务器端口所做的一样,同样应该定义这个键值对
    • @SyedZeeshanAli 用户代码如下。我确认它正在工作。 ` sping:jpa:休眠:命名:物理策略:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl `
    【解决方案2】:

    对于休眠 v5

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

    【讨论】:

      【解决方案3】:

      在你的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】:

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

        【讨论】:

          【解决方案5】:

          使用

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

          【讨论】:

            【解决方案6】:

            application.properties集合中

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

            【讨论】:

              【解决方案7】:

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

              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 正在使用中,并且您有下划线...

              【讨论】:

                【解决方案8】:
                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
                

                【讨论】:

                  【解决方案9】:

                  解决了。

                  无效的对象名称:Springboot with JPA(SQL server)

                  在 application.yaml/properties 中指定

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

                  jpa: 显示 SQL:假 休眠: ddl-auto: none # 不在嵌入模式时默认为“none” 命名: 隐式策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl 物理策略:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2014-06-13
                    • 2013-01-05
                    • 1970-01-01
                    • 2012-12-14
                    • 1970-01-01
                    • 2015-10-07
                    • 1970-01-01
                    • 2012-10-08
                    相关资源
                    最近更新 更多