简体   繁体   English

移动Spring Boot 1.3到1.4,Hibernate 4到5,Pascal案例问题

[英]Moving Spring Boot 1.3 to 1.4, Hibernate 4 to 5, Pascal Case Issues

I created a Spring Boot 1.3.5 POC with Spring Data JPA and Hibernate (4.3.11.Final in this version of Spring Boot). 我用Spring Data JPA和Hibernate创建了一个Spring Boot 1.3.5 POC(这个版本的Spring Boot中的4.3.11.Final)。 My backend database is Microsoft SQL Server, and our standard naming convention for database objects is pascal case (eg MySchema.MyTable.MyColumn). 我的后端数据库是Microsoft SQL Server,我们对数据库对象的标准命名约定是pascal case(例如MySchema.MyTable.MyColumn)。 I used the javax.persistence.Table and javax.persistence.Column annotations to set the names, and added spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy to my application.properties file. 我使用javax.persistence.Table和javax.persistence.Column注释来设置名称,并将spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.EJB3NamingStrategy添加到我的application.properties文件中。

Everything worked perfectly. 一切都很完美。 I even updated to Spring Boot 1.3.6 with no issues. 我甚至更新到Spring Boot 1.3.6没有任何问题。

Now I moved to Spring Boot 1.4.0.RELEASE which uses Hibernate 5.0.9.Final, and the spring.jpa.hibernate.naming-strategy property is deprecated in favor of spring.jpa.hibernate.naming.strategy. 现在我转到使用Hibernate 5.0.9.Final的Spring Boot 1.4.0.RELEASE,并且不推荐使用spring.jpa.hibernate.naming-strategy属性,而选择spring.jpa.hibernate.naming.strategy。 I changed that property name, but left the EJB3NamingStrategy value. 我更改了该属性名称,但保留了EJB3NamingStrategy值。 I also changed the other deprecated elements: 我还更改了其他已弃用的元素:

  • org.springframework.boot.orm.jpa.EntityScan to org.springframework.boot.autoconfigure.domain.EntityScan org.springframework.boot.orm.jpa.EntityScan to org.springframework.boot.autoconfigure.domain.EntityScan
  • org.springframework.boot.context.web.SpringBootServletInitializer to org.springframework.boot.web.support.SpringBootServletInitializer org.springframework.boot.context.web.SpringBootServletInitializer到org.springframework.boot.web.support.SpringBootServletInitializer
  • org.springframework.boot.test.SpringApplicationConfiguration to org.springframework.boot.test.context.SpringBootTest (in my test classes) org.springframework.boot.test.SpringApplicationConfiguration to org.springframework.boot.test.context.SpringBootTest(在我的测试类中)

Now the generated SQL uses the default camel case to underscore naming convention and not the pascal case that I had with EJB3NamingStrategy. 现在,生成的SQL使用默认的camel情况来强调命名约定,而不是我使用EJB3NamingStrategy的pascal情况。

//application.properties
spring.data.jpa.repositories.enabled=true
spring.data.solr.repositories.enabled=false
spring.data.mongodb.repositories.enabled=false
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.EJB3NamingStrategy
#spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy

//hibernate.properties
hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
hibernate.format_sql=true

//Principal.java
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name="Principal", schema="Security")
@Audited
@AuditTable(value = "Principal", schema = "Audit")
public class Principal {

    private static final Logger LOG = LoggerFactory.getLogger(Principal.class);

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id", 
            nullable = false)
    private Long id;

    @Column(name = "Username", 
            nullable = false, 
            unique = true)
    @Size(min = 1, max = 64)
    private String name;

    @Column(name = "FirstName", 
            nullable = false)
    @Size(min = 1, max = 64)
    private String firstName;

    @Column(name = "LastName", 
            nullable = false)
    @Size(min = 1, max = 128)
    private String lastName;

    @Column(name = "IsEnabled", 
            nullable = false)
    private boolean enabled;

    //getters/setters omitted for brevity
}

orignal console output: 原始控制台输出:

Hibernate: 
    select
        principal0_.Id as Id1_8_,
        principal0_.IsEnabled as IsEnable2_8_,
        principal0_.FirstName as FirstNam3_8_,
        principal0_.LastName as LastName4_8_,
        principal0_.Username as Username5_8_ 
    from
        Security.Principal principal0_ 
    where
        principal0_.Username=?

new console output: 新的控制台输出:

Hibernate: 
    select
        principal0_.id as id1_7_,
        principal0_.is_enabled as is_enabl2_7_,
        principal0_.first_name as first_na3_7_,
        principal0_.last_name as last_nam4_7_,
        principal0_.username as username5_7_ 
    from
        security.principal principal0_ 
    where
        principal0_.username=?
2016-08-05 09:19:22.751  WARN 5032 --- [  XNIO-2 task-8] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 207, SQLState: S0001
2016-08-05 09:19:22.751 ERROR 5032 --- [  XNIO-2 task-8] o.h.engine.jdbc.spi.SqlExceptionHelper   : Invalid column name 'is_enabled'.
2016-08-05 09:19:22.768 ERROR 5032 --- [  XNIO-2 task-8] io.undertow.request                      : UT005023: Exception handling request to /springbootsecurity/login

I've searched extensively, and found references to ImplicitNamingStrategy and PhysicalNamingStrategy; 我进行了广泛的搜索,发现了对ImplicitNamingStrategy和PhysicalNamingStrategy的引用; but plugging those in don't seem to work and is probably not the correct approach. 但插入那些似乎不起作用,可能不是正确的方法。 I've also seen references to creating my own NamingStrategy. 我也看到了创建自己的NamingStrategy的参考。 Is that the route I must take? 那是我必须走的路吗?

Is there a different setting for Hibernate 5 that will use the exact name I provide in the @Table and @Column annotations? 是否有一个不同的Hibernate 5设置,它将使用我在@Table和@Column注释中提供的确切名称? Is there a problem with the way I am defining the annotations? 我定义注释的方式有问题吗?

I would like to say I ended up posting a silly question, but every direction I went talked about creating a custom naming strategy. 我想说我最后发布了一个愚蠢的问题,但我谈到了创建自定义命名策略的每一个方向。 The answer in my case, however, was simply using Hibernate's PhysicalNamingStrategyStandardImpl. 然而,我的答案就是简单地使用Hibernate的PhysicalNamingStrategyStandardImpl。

Added to application.properties: 添加到application.properties:

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

From my naive analysis, I'm assuming this works because I am using the @Table and @Column annotations. 从我天真的分析,我假设这是有效的,因为我使用@Table和@Column注释。 The PhysicalNamingStrategyStandardImpl appears to simply use the name in those annotations as the database object name. PhysicalNamingStrategyStandardImpl似乎只是将这些注释中的名称用作数据库对象名称。

So my Hibernate generated query is once again formatted as: 所以我的Hibernate生成的查询再次格式化为:

Hibernate: 
    select
        principal0_.Id as Id1_7_,
        principal0_.IsEnabled as IsEnable2_7_,
        principal0_.FirstName as FirstNam3_7_,
        principal0_.LastName as LastName4_7_,
        principal0_.Username as Username5_7_ 
    from
        Security.Principal principal0_ 
    where
        principal0_.Username=?

Reading @AmanTuladhar's link and this link from that post is where it eventually clicked for me. 阅读@AmanTuladhar的链接帖子中的这个链接是它最终点击给我的地方。 Thanks! 谢谢!

this is really a nice thread , for a beginner - who are migrating from spring boot 1.3 to 1.4 - below link contains all the stranded changes required , it also list all the deprecated options and contains some examples as well . 这对于初学者来说真的是一个很好的线程 - 谁从春季启动1.3迁移到1.4 - 链接下面包含所需的所有搁浅的更改,它还列出了所有已弃用的选项,并包含一些示例。

It gives overview about almost everything that you can use with application. 它概述了几乎可以与应用程序一起使用的所有内容。 For ex- Hibernate , Log4j , Junit/Mockito , Integration and so on . 对于ex-Hibernate,Log4j,Junit / Mockito,Integration等。 Please follow below link 请点击以下链接

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM