JPA How to Set Table Name and Entity Name Table Field and Entity Field Correspondence
- 2021-12-12 08:46:38
- OfStack
JPA sets the table name and entity name, and the correspondence between the table field and the entity field
First of all, your jpaProperties configuration item should have
<prop key="hibernate.hbm2ddl.auto">update</prop>
In this way, objects can be directly mapped into table structure, and object-oriented can be transformed into database.
Entity names can be different from table names, and field names can be different from entity names.
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")// Set the table name in the database
public class ShiroUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="USER_NAME")<span style="font-family: Arial, Helvetica, sans-serif;">// Set the name of the field in the database, and also set the attributes such as length and whether it is empty or not </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 Database Table Entity Naming Rules
Unknown column 'user0_.create_time' in 'field list'
The database and table fields are named by hump nomenclature (createTime). When Spring data jpa operates the table, the generated sql statement is create_time, and the table fields are not compared.
Spring data jpa is based on hibernate-core-5. 2.16. final. jar
This is caused by the field full name policy of hibernate in jpa, which is SpringPhysicalNamingStrategy by default. There is a 1-heap naming policy in the package of hibernate: org. hibernate. boot. model. naming, which can also be implemented
PhysicalNamingStrategy custom implementation.
application. properties
1. Naming without modification
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
2. Encounter the naming of capital letters plus "_"
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy