今天在写Spring Data JPA的时候发现这样一个问题
问题如下:
Caused by: org.hibernate.DuplicateMappingException: Table [sys_user_role] contains physical
column name [user_role_id] referred to by multiple logical column names:
[user_role_id], [userRoleId]
Sys_User 实体类代码
@Entity
//实体与表名对应
@Table(name = "sys_user")
public class Sys_User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//generator = "identity"递增的意思
@Column(name = "user_id")
private String userId;//用户编号(人员编号(实验室助理)等)
private String userName;//用户名
private String password;//密码
private String major;//专业
private String institute;//学院
private String classes;//班级
private String grade;//年级
private String telephone;//手机号
private String email;//邮箱
private String imageId;//个人照片
private String labId;//所属实验室
/*
配置一对多的关系-----Sys_User_Role
cascade设置级联属性,同步添加,同步删除
*/
@OneToMany(targetEntity = Sys_User_Role.class,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "user_role_id",referencedColumnName = "user_id")//name代表外键的名称,referencedColumnName代表的是主表即当前表的主键名称
private Set<Sys_User_Role> sys_user_roles = new HashSet<Sys_User_Role>();
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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 String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getInstitute() {
return institute;
}
public void setInstitute(String institute) {
this.institute = institute;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public String getLabId() {
return labId;
}
public void setLabId(String labId) {
this.labId = labId;
}
public Set<Sys_User_Role> getSys_user_roles() {
return sys_user_roles;
}
public void setSys_user_roles(Set<Sys_User_Role> sys_user_roles) {
this.sys_user_roles = sys_user_roles;
}
}
Sys_User_Role 实体类代码
@Entity
@Table(name = "sys_user_role")
public class Sys_User_Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;//主键id
private Integer userRoleId;//外键
private Integer roleId;//用户类型(1:实验技术员,2:实验室助理,:学工处人员;4系统用户(学生))
//配置多对一关系--Sys_user
@ManyToOne(targetEntity = Sys_User.class,fetch = FetchType.LAZY)
@JoinColumn(name = "user_role_id",referencedColumnName = "user_id",
insertable = false,updatable = false)
private Sys_User sys_user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public Sys_User getSys_user() {
return sys_user;
}
public void setSys_user(Sys_User sys_user) {
this.sys_user = sys_user;
}
public Integer getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(Integer userRoleId) {
this.userRoleId = userRoleId;
}
}
这个问题困扰了我一天,当我设置一对多,多对一双向关联关系的时候,发现总是报错,报错信息跟外键有关,调试了之后还会报一些字段重复的错误。在网上找了一些例子看了之后,发现别人写的代码,辅表实体类都不会把外键写出来,我这里外键的名字是user_role_id,只要把Sys_User_Role 里面的外键删除即可,正确的实体类代码如下:
@Entity
@Table(name = "sys_user_role")
public class Sys_User_Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;//主键id
private Integer roleId;//用户类型(1:实验技术员,2:实验室助理,:学工处人员;4系统用户(学生))
//配置多对一关系--Sys_user
@ManyToOne(targetEntity = Sys_User.class,fetch = FetchType.LAZY)
@JoinColumn(name = "user_role_id",referencedColumnName = "user_id",
insertable = false,updatable = false)
private Sys_User sys_user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public Sys_User getSys_user() {
return sys_user;
}
public void setSys_user(Sys_User sys_user) {
this.sys_user = sys_user;
}
}