小编典典

Spring Boot的Hibernate字段命名问题(命名策略)

spring-boot

请注意,此代码确实适用于普通Spring,但不适用于Spring
Boot(v1.3.3),我缺少什么东西了,因为这是从有效的Spring应用程序导入的。下面的代码来自spring boot应用

@Entity
@Table(name="project")
public class Project implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name="teamId")
    private int teamId;

    //private String Rentabiliteit;

    @Column
    //@Index(name="IProject_status",columnNames="Status")
    private String status;

    @Column
    //@Index(name="IProject_naam",columnNames="Naam")
    private String naam;
    //public Prototype m_Prototype;
    //public Team m_Team;

}

的SQL

CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL,
`teamId` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`naam` varchar(255) DEFAULT NULL
 ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=latin1;

错误

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:           
 Unknown column 'project0_.team_id' in 'field list'

编辑:Application.yml

spring:

mvc:
  view:
    prefix: /WEB-INF/jsp/
    suffix: .jsp

datasource:
    url: jdbc:mysql://localhost:3306/oxyplast
    username: oxyplastuser
    password: oxyplastuserpw

jpa:
  properties:
    hibernate:
      current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext 
      namingStrategy: org.hibernate.cfg.DefaultNamingStrategy

阅读 535

收藏
2020-05-30

共1个答案

小编典典

从1.4版本开始

从1.4开始,因为切换到hibernate5日,命名策略已经更新到SpringPhysicalNamingStrategy应该是非常接近
1.3的默认值。

也可以看看:


以前的版本

Spring
Boot提供了ImprovedNamingStrategy默认的命名策略,该策略使Hibernate搜索team_id列(从int teamId字段推断)。由于此列在您的表中不存在,因此是错误的原因。从Hibernate文档中:

一种改进的命名策略,它首选使用嵌入式下划线而不是大小写混合的名称

您有两种选择:

  1. 将列名显式提供@Column(name="teamId")。在早期的Boot版本中曾经有一个bug,现在不再存在。

  2. *在Spring Boot属性中 *更改命名策略 ,并告诉它使用EJB3NamingStrategy,这不会将camelCase转换为snake_case,而是保持原样。

2020-05-30