0
我想在@一對多要使用映射,但它拋出一個異常休眠的mappedBy複合鍵
org.hibernate.MappingException: Unable to read the mapped by attribute for moduleRoles in org.caau.entity.UserModuleRole!
這裏是我的實體mapping.UserModuleRole是複合class.if使用註釋代碼,程序是correct.Does有人幫我解決?
@Entity
@Table(name = "user")
public class User{
private long id;
private Set<UserModuleRole> moduleRoles;
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
@OneToMany(fetch = FetchType.EAGER,mappedBy="user")
//if use under code,program is correct,but i want to use mappedBy!
//@OneToMany(fetch = FetchType.EAGER)
//@JoinColumn(name = "user_id")
public Set<UserModuleRole> getModuleRoles() {
return moduleRoles;
}
}
這裏是類UserModuleRole
@Entity
@Table(name = "user_module_role")
@IdClass(UserModuleRolePK.class)
public class UserModuleRole{
private User user;
private ModuleRole moduleRole;
private long addUserId;
private Date addDate;
private long updateUserId;
private Date updateDate;
@Id
public User getUser() {
return user;
}
@Id
public ModuleRole getModuleRole() {
return moduleRole;
}
}
class UserModuleRolePK implements Serializable {
private static final long serialVersionUID = -9132981262254922539L;
private User user;
private ModuleRole moduleRole;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
public User getUser() {
return user;
}
@ManyToOne
@JoinColumns({ @JoinColumn(name = "module_id", nullable = false), @JoinColumn(name = "role_id", nullable = false) })
public ModuleRole getModuleRole() {
return moduleRole;
}
}
}
但我user_module_role有其他領域 創建表user_module_role( \t的user_id INT無符號不爲空, \t是模塊INT無符號不爲空, \t ROLE_ID INT無符號不爲空, \t add_user_id INT無符號的默認值是1, add_date日期時間默認'1970/01/01', update_user_id int unsigned default 1, update_date datetime default'1970/01/01'); –
您是否檢查了我在答案末尾放置的教程鏈接? –