首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动Spring 1.3到1.4,Hibernate 4到5,Pascal案例问题

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

Stack Overflow用户
提问于 2016-08-05 16:52:23
回答 2查看 2.8K关注 0票数 5

我使用Spring和Hibernate创建了一个SpringBoot1.3.5POC(这个版本的Spring中为4.3.11.Final)。我的后端数据库是Microsoft,我们对数据库对象的标准命名约定是pascal (例如MySchema.MyTable.MyColumn)。我使用javax.persistence.Table和javax.persistence.Column注释来设置名称,并将javax.persistence.Table添加到application.properties文件中。

一切都很完美。我甚至更新到SpringBoot1.3.6,没有任何问题。

现在我转到SpringBoot1.4.0.RELEASE,它使用Hibernate 5.0.9.Final,推荐使用spring.jpa.hibernate.naming.strategy。我更改了该属性名,但保留了EJB3NamingStrategy值。我还更改了其他不推荐的元素:

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

现在,生成的SQL使用默认的camel大小写来强调命名约定,而不是使用EJB3NamingStrategy的pascal大小写。

代码语言:javascript
复制
//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
}

原始控制台输出:

代码语言:javascript
复制
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=?

新控制台输出:

代码语言:javascript
复制
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

我已经进行了广泛的搜索,并发现了对ImplicitNamingStrategy和PhysicalNamingStrategy的引用;但是插入它们似乎不起作用,而且可能不是正确的方法。我还看到了关于创建我自己的NamingStrategy的引用。这是我必须走的路线吗?

是否有不同的Hibernate 5设置将使用我在@Table和@Column注释中提供的确切名称?我定义注释的方式有问题吗?

EN

回答 2

Stack Overflow用户

发布于 2016-08-05 20:48:00

我想说的是,我最后发布了一个愚蠢的问题,但我谈到的每一个方向都是关于创建自定义命名策略的。然而,在我的例子中,答案是简单地使用Hibernate的PhysicalNamingStrategyStandardImpl。

添加到application.properties中:

代码语言:javascript
复制
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

从我天真的分析来看,我假设这是因为我使用的是@Table和@Column注释。PhysicalNamingStrategyStandardImpl似乎只是将这些注释中的名称用作数据库对象名称。

因此,我的Hibernate生成的查询再次格式化为:

代码语言:javascript
复制
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=?

从那篇文章中读到@AmanTuladhar的链接此链接是我最终点击的地方。谢谢!

票数 8
EN

Stack Overflow用户

发布于 2018-07-02 13:46:01

这真是一个不错的线程,对于初学者来说--他们正在从spring 1.3迁移到1.4 --下面的链接包含了所需的所有搁浅更改,它还列出了所有不推荐的选项,并包含了一些示例。

它概述了几乎所有您可以在应用程序中使用的内容。用于前Hibernate、Log4j、Junit/Mockito、集成等。请按以下连结

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38794253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档