我有一個包含兩列的組合主鍵的表,並且該表也具有唯一鍵。另一個表中的唯一鍵映射爲外鍵。如何用Hibernate實現這個映射?我爲複合主鍵創建了一個@Embedable類,並且在實體中使用了@EmbeddedId註解。在第二個表,我宣佈一個字段:休眠:映射指向唯一鍵的外鍵
@ManyToOne
@JoinColumn(name = "user_code", nullable = false)
public User userCode;
這是從第一個表中的唯一關鍵,但我發現了異常:從com.users外鍵闖民宅com.users.maintenance.User .maintenance.Code列數錯誤。應該是2。
編輯 這是我的用戶等級:
@Entity
@Table(name = "Users")
public class User {
@EmbeddedId
public UserPk userId;
@column(name = "code")
public int code;
public UserPk getUserId() {
return this.userId;
}
public void setUserId(UserPk userId) {
this.userId = userId;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Embeddable
private class UserPk implements Seriazible {
@Column(name = "user_name")
public String userName;
@Column(name = "email")
public String email;
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
return this.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof UserPk)) {
return false;
}
UserPk pk = (UserPk)obj;
return pk.userName == userName && pk.email == email;
}
}
}
請顯示用戶級別 –