【问题标题】:Wrong column name even if specified [duplicate]即使指定了错误的列名[重复]
【发布时间】:2016-07-05 08:43:09
【问题描述】:

我正在使用 Spring Data JPA + Hibernate 进行数据访问的 Spring Boot 项目,但我遇到了一个奇怪的问题。

在我的实体类中,我指定了这些列:

@Entity
@Table(name = "Subjects")
public class Subject {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    @Column(name="InsertionDate")
    private Date insertDate;

    // get + set...
}

当我尝试执行 repo.findAll() 时,我得到了这个异常:

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'insertion_date'.

我的配置有什么问题?为什么 Hibernate 搜索 'insertion_date' 而不是 'InsertionDate'?如何配置正确的列名?

【问题讨论】:

  • 改成@Column(name="xxxDate")private Date insertDate;会有什么好处?
  • 尽量保持列名小写。一些命名策略应用了一些驼峰式转换......

标签: java spring hibernate jpa spring-boot


【解决方案1】:

在 SpringBoot 中,@Column(name="") 注释将被忽略,除非您将应用程序属性设置为使用不同的命名类。

如果您没有application.properties,请在您的src/main/resources 目录中创建一个具有该名称的文件。添加行:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

在其中,您的命名将随心所欲。

参考:Spring Boot + JPA : Column name annotation ignored

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多