Spring Boot的Hibernate字段命名问题(命名策略) [英] Hibernate field naming issue with Spring Boot (naming strategy)

查看:331
本文介绍了Spring Boot的Hibernate字段命名问题(命名策略)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这段代码对普通的Spring不起作用,但对Spring Boot(v1.3.3)不起作用,是否有我缺少的东西,因为这是从一个有效的弹簧应用程序导入的。下面的代码是从春季启动应用程序

  @Entity 
@Table(name =project)
public class Project实现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;

$ b $ / code $ / pre
$ b $ SQL $ /
$ b $ (
`id` int(11)NOT NULL,
`teamId` int(11)DEFAULT NULL,$

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

错误

 造成者:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
'字段列表'中未知列'project0_.team_id'

编辑:Application.yml

  spring:

mvc:
查看:
前缀:/ WEB-INF / jsp /
后缀:.jsp

数据源:
url:jdbc: mysql:// localhost:3306 / oxyplast
用户名:oxyplastuser
密码:oxyplastuserpw
$ b jpa:
属性:
hibernate:
current_session_context_class :org.springframework.orm.hibernate4.SpringSessionContext
namingStrategy:org.hibernate.cfg.DefaultNamingStrategy


ImprovedNamingStrategy 作为默认的命名策略,这使得Hibernate搜索 team_id 列(从 int teamId 字段推断出来)。由于该列在您的表中不存在,所以这是错误的原因。从Hibernate文档:


改进的命名策略,首选嵌入式下划线混合大小写名称




您有两种选择:


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

  2. 改变Spring Boot属性中的命名策略,并告诉它使用 EJB3NamingStrategy ,其中不会将camelCase转换为snake_case,但会保持原样。







更新



从1.4开始,由于切换到Hibernate 5,命名策略已更新为 SpringPhysicalNamingStrategy 其中应该非常接近到1.3默认值。



另请参阅: =https://stackoverflow.com/a/25293929/1199132> Spring的命名策略

Note that this code does work with plain Spring but not with Spring Boot(v1.3.3), is there something i'm missing because this is imported from a spring app that works. The code below is from the spring boot app

@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;

ERROR

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

Edited: 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

解决方案

Spring Boot provides the ImprovedNamingStrategy as default naming strategy, which makes Hibernate search for a team_id column (inferred from the int teamId field). As this column doesn't exist in your table, that's the cause of the error. From the Hibernate docs:

An improved naming strategy that prefers embedded underscores to mixed case names

You've got two options:

  1. Provide the column name explicitly as @Column(name="teamId"). There used to be a bug with this in early Boot versions, not anymore.

  2. Change the naming strategy in the Spring Boot properties and tell it to use the EJB3NamingStrategy, which doesn't convert camelCase to snake_case, but keeps it as it is.


Update

Starting from 1.4, because of the switch to Hibernate 5, the naming strategy has been updated to SpringPhysicalNamingStrategy which should be very close to 1.3 defaults.

See also:

这篇关于Spring Boot的Hibernate字段命名问题(命名策略)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆