在使用spring jpa 访问数据库表时,在实体类上定义的@column 会无效,
package com.lujia.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
* @author :lujia
* @date :2018/11/16 10:22
*/
@Entity
@Table(name = "redis_role")
public class RedisRole implements Serializable{
private static final long serialVersionUID = -3722607639305675814L;
@Id
private Long id;
/**
* @Column 注解无效
*/
@Column(name = "mgmtName")
private String mgmtName;
private String remark;
@Column(name = "createDate")
private Date createDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getMgmtName() {
return mgmtName;
}
public void setMgmtName(String mgmtName) {
this.mgmtName = mgmtName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
,直接访问数据库会报异常Unknown column 'redisrole0_.create_date' in 'field list' ,默认帮我们把列名给整成create_date这
种,所以回报异常,此时我们可以指定他的naming strategry ,
PhysicalNamingStrategyStandardImpl 类里面,会直接使用字段名和列名映射,就行了
网上有些资料说的是指定这个属性,jpa:hibernate:naming:strategy: org.hibernate.cfg.ImprovedNamingStrategy
然后扩展ImprovedNamingStrategy 类,但是我试了,好像是不行的,不知道是不是我的方法不正确
spring:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.boot.model.naming;
import java.io.Serializable;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
/**
* Standard implementation of the PhysicalNamingStrategy contract.
*
* @author Steve Ebersole
*/
public class PhysicalNamingStrategyStandardImpl implements PhysicalNamingStrategy, Serializable {
/**
* Singleton access
*/
public static final PhysicalNamingStrategyStandardImpl INSTANCE = new PhysicalNamingStrategyStandardImpl();
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment context) {
return name;
}
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment context) {
return name;
}
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return name;
}
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
return name;
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return name;
}
}