In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍"JPA怎么设置表名和实体名",在日常操作中,相信很多人在JPA怎么设置表名和实体名问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"JPA怎么设置表名和实体名"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
JPA设置表名和实体名,表字段与实体字段的对应
首先 你的jpaProperties配置项中要有
update
这样就可以直接有对象映射为表结构,实现面向对象向数据库转化。
实体的名字可以和表名字不一样,字段名字和实体的名字也可以不一样。
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")//设置数据库中字段名字,也可以设置长度,是否为空等属性 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
到此,关于"JPA怎么设置表名和实体名"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.