如何设置hibernate中的多个many to many映射关系?使用的是注解写的,求解!

2024-12-04 12:35:55
推荐回答(4个)
回答1:

你没说具体的实体和关联关系,只能给你举个例子。
比如Student 和Teacher 是多对多关系
数据库中有三张表,student,teacher,student_teacher
在Student中
@Entity
@Table(name="student")
public class Student {
@Id //id的生成策略自己加
private String studentId;
……其他省略
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="student_teacher", //这里的name是数据库中间表名
joinColumns={@JoinColumn(name="studentId")}, //本表在中间表的外键名称 inverseJoinColumns={@JoinColumn(name="teacherId")})
private Set teachers;
……
}

Teacher中
@Entity
@Table(name="teacher")
public class Teacher{
@Id
private String teacherId;
……其他省略
//如果是单向关系,teacher中不要求找student,那么Teacher中就不用student的Set了
//双向关系的话就加也加上Set
@ManyToMany(mappedBy="teachers") //指Student中的teachers 也就是说关系映射teachers
private Set students;
……
不知道你具体问题。只能给你个简单例子。如有需要可以Hi百度我

回答2:

参见http://blog.myspace.cn/e/402334665.htm
里面介绍了hibernate的多种映射关系。
希望能帮到你

回答3:

@Entity
@Table(name="users")
public class Users {
private int userId;
private String username;
private String name;
private String password;
private String telephone;
private Date lastOnLoadTime;
private String CRM_username;
private String CRM_password;
private List roles;
private String dataRole;
private String isDele;
private String mark;

@Id
@GeneratedValue
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getLastOnLoadTime() {
return lastOnLoadTime;
}
public void setLastOnLoadTime(Date lastOnLoadTime) {
this.lastOnLoadTime = lastOnLoadTime;
}
public String getCRM_username() {
return CRM_username;
}
public void setCRM_username(String cRMUsername) {
CRM_username = cRMUsername;
}
public String getCRM_password() {
return CRM_password;
}
public void setCRM_password(String cRMPassword) {
CRM_password = cRMPassword;
}
@ManyToMany
public List getRoles() {
return roles;
}
public void setRoles(List roles) {
this.roles = roles;
}
public String getDataRole() {
return dataRole;
}
public void setDataRole(String dataRole) {
this.dataRole = dataRole;
}
public String getIsDele() {
return isDele;
}
public void setIsDele(String isDele) {
this.isDele = isDele;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
}

回答4:

0.0 SET?