我想在@OneToMany 中使用映射,但它会引发异常
org.hibernate.MappingException: Unable to read the mapped by attribute for moduleRoles in org.caau.entity.UserModuleRole!
这是我的实体映射。UserModuleRole 是复合类。如果使用注释代码,程序是正确的。有人帮我解决吗?
@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;
}
}
}