Reputation: 109
I want to use mapped in @OneToMany,but it throws an exception
org.hibernate.MappingException: Unable to read the mapped by attribute for moduleRoles in org.caau.entity.UserModuleRole!
And here is my entity mapping.UserModuleRole is composite class.if use comment code ,program is correct.Does anyone help me to solve?
@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;
}
}
here is class 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;
}
}
}
Upvotes: 0
Views: 3056
Reputation: 7858
First, the exception you are getting is due to the fact that there is not any relational attribute named user
in your UserModuleRole
class.
What you are trying to achieve is a Many-To-Many relationship between your entities User
and ModuleRole
, the intermediary class UserModuleRole
is not needed in your example as the only fields/properties it holds are the foreign key references to the other two entities.
Unless you forgot to add more information to your question, the mapping you should use is much simpler, something like the following:
@Entity
public class ModuleRole {
private Long id;
private Set<User> users;
@Id
@Column(name = "id")
public Long getId() {
return id;
}
@ManyToMany
@JoinTable(name = "user_module_role",
joinColumns = @JoinColumn(name = "module_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id"), referencedColumnName = "id")
public Set<Users> getUsers() {
return users;
}
}
@Entity
@Table(name = "user")
public class User{
private Long id;
private Set<ModuleRole> moduleRoles;
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
@ManyToMany(mappedBy = "users")
public Set<ModuleRole> getModuleRoles() {
return moduleRoles;
}
}
Hibernate will deal with the intermediary table on its own, you don't have to worry about it unless you want to add additional fields to the relationship, in that case this tutorial would help.
Upvotes: 1