解决springdatajpa 在解析实体类的字段时候驼峰自动转为下划线问题
问题描述
springboot 在配置jpa时,利用idea自带工具逆向工程生成实体类,结果在调用findOne的时候,报错
错误内容如下所示:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'shasset0_.asset_type_name' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_80]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_80]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_80]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_80]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.47.jar:5.1.47]
实体类片段
@Entity
@Table(name = "sh_asset")
public class ShAsset {
private String id;
private String assetTypeName;
private String assetName;
private String assetCode;
private String assetType;
private String area;
private String orgname;
private String username;
private String contract;
private String address;
private String recorderId;
private String recorderName;
private Timestamp recorddate;
private String iPadd;
private Timestamp editTime;
private Integer assetStatus;
private String maCadd;
private Date userDate;
private Integer leaseState;
@Id
@Column(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Basic
@Column(name = "asset_typeName")
public String getAssetTypeName() {
return assetTypeName;
}
public void setAssetTypeName(String assetTypeName) {
this.assetTypeName = assetTypeName;
}
问题原因
由于springboot 中jpa默认自动将实体类中的驼峰命名转化为下划线隔开的方式进行数据库查询,将字段
getAssetTypeName
自动转换为asset_type_name
导致数据库找不到该字段
解决办法
在配置文件中取消自动将驼峰命名转为下划线形式
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl