JPA如何设置表名和实体名,表字段与实体字段的对应
作者:LQW_home 时间:2022-07-14 14:36:27
JPA设置表名和实体名,表字段与实体字段的对应
首先 你的jpaProperties配置项中要有
<prop key="hibernate.hbm2ddl.auto">update</prop>
这样就可以直接有对象映射为表结构,实现面向对象向数据库转化。
实体的名字可以和表名字不一样,字段名字和实体的名字也可以不一样。
package com.shiroweb.entitys;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="shiro_user")//设置数据库中表名字
public class ShiroUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="USER_NAME")<span style="font-family: Arial, Helvetica, sans-serif;">//设置数据库中字段名字,也可以设置长度,是否为空等属性</span>
private String username;
@Column(name="PASS_WORD")
private String password;
@Column(name="CREATE_DATE")
private Date createDate;
@Column(name="USER_ID")
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
JPA数据库表实体命名规则
Unknown column 'user0_.create_time' in 'field list'
数据库,表字段命名是驼峰命名法(createTime),Spring data jpa 在操作表的时候,生成的sql语句中却是create_time, 表字段不对照,
Spring data jpa基于hibernate-core-5.2.16.final.jar
这是由于jpa中hibernate的字段全名策略引起的,默认是SpringPhysicalNamingStrategy,在hibernate的包:org.hibernate.boot.model.naming中有一堆的命名策略,当然也可以实现
PhysicalNamingStrategy自定义实现。
application.properties 写法
1、无修改命名
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
2、遇到大写字母 加”_”的命名
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
来源:https://blog.csdn.net/LQW_java_home/article/details/53079363
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中使用imageviewswitcher 实现图片切换轮播导航的方法
![](https://img.aspxhome.com/file/2023/0/138040_0s.gif)
Java版的7种单例模式写法示例
Kotlin定义其他类的实现详解
![](https://img.aspxhome.com/file/2023/3/138163_0s.png)
Unity3D中脚本的执行顺序和编译顺序
![](https://img.aspxhome.com/file/2023/9/76419_0s.png)
Java重写与重载之间的区别
C#子线程更新UI控件的方法实例总结
![](https://img.aspxhome.com/file/2023/9/81089_0s.jpg)
struts2自定义拦截器的示例代码
![](https://img.aspxhome.com/file/2023/3/78243_0s.jpg)
c#多线程之间的排他锁的实现
Android ViewPager实现无限循环效果
![](https://img.aspxhome.com/file/2023/2/122112_0s.gif)
java之swing表格实现方法
Android开发之使用ViewPager实现图片左右滑动切换效果
Spring的连接数据库以及JDBC模板(实例讲解)
![](https://img.aspxhome.com/file/2023/1/63101_0s.png)
Spring Security登录表单配置示例详解
![](https://img.aspxhome.com/file/2023/0/67670_0s.png)
C# Resources资源详解
Spring boot中PropertySource注解的使用方法详解
![](https://img.aspxhome.com/file/2023/9/72539_0s.jpg)
Java实现调用对方http接口得到返回数据
![](https://img.aspxhome.com/file/2023/7/101777_0s.png)
java使用Dijkstra算法实现单源最短路径
![](https://img.aspxhome.com/file/2023/4/96134_0s.jpg)
java导出数据库的全部表到excel
Android Service启动过程完整分析
![](https://img.aspxhome.com/file/2023/9/137989_0s.jpg)
Android多媒体应用使用MediaPlayer播放音频
![](https://img.aspxhome.com/file/2023/7/102607_0s.jpg)