我想使用hibernate查询由内连接连接的两个表。
两个表格如下。
CREATE TABLE systemuser
(
system_user_id
INT(20)NOT NULL AUTO_INCREMENT,
user_name
VARCHAR(50)NOT NULL DEFAULT'0',
主要关键(system_user_id
)
)
CREATE TABLE role
(
system_user_id
INT(20)NOT NULL,
role_id
INT(20)NOT NULL DEFAULT'0',
PRIMARY KEY(system_user_id,role_id
),
约束FK_SYSTEM_USER_ID
外键(system_user_id
)参考systemuser
(system_user_id
)
)
这两个表有一对多的关系。
这些丝束的Bean类如下。
@Entity(name = "role")
@IdClass(RoleCompositeKey.class)
public class Role extends implements java.io.Serializable {
private int roleId =0;
private int systemUserId =0;
private SystemUser systemUser;
public Role(){
}
public void setRoleId(int roleId){
this.roleId = roleId;
}
@Id
@Column(name = "role_Id")
public int getRoleId(){
return roleId;
}
public void setSystemUserId(int systemUserId){
this.systemUserId = systemUserId;
}
@Id
@Column(name="system_user_id")
public int getSystemUserId(){
return systemUserId;
}
public void setSystemUser(SystemUser systemUser){
this.systemUser= systemUser;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "system_user_id", nullable = false)
public SystemUser getSystemUser(){
return systemUser;
}
}
@Entity
@Table(name = "systemuser",uniqueConstraints = {
@UniqueConstraint(columnNames = "system_user_id"),
@UniqueConstraint(columnNames = "user_name") })
public class SystemUser {
private int systemUserId = 0;
private String userName = null;
private Set<Role> roleList;
public SystemUser(){
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "user_name")
public String getUserName() {
return userName;
}
public void setSystemUserId(int systemUserId){
this.systemUserId = systemUserId;
}
@Id
@Column(name="system_user_id")
public int getSystemUserId(){
return systemUserId;
}
public void setRole(Set<Role> roleList){
this.roleList = roleList;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "systemUser")
public Set<Role> getRole(){
return roleList;
}
}
但我收到错误
“........构造函数抛出异常;嵌套异常是org.hibernate.MappingException:实体映射中的重复列:.... beans.Role列:system_user_id(应使用insert =”映射) false“update =”false“)”
请有人帮帮我吗?任何帮助都非常感谢!
提前致谢!
答案 0 :(得分:0)
&#34;实体映射中的重复列&#34;。你在角色中声明两次关系。删除这个。
@Id
@Column(name="system_user_id")
public int getSystemUserId(){
return systemUserId;
}
然后离开这个
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "system_user_id", nullable = false)
public SystemUser getSystemUser(){
return systemUser;
}