(4) 1:n 单向关联
1:n 单向关联是指在1的那方的类里存在n那方的对象(多个)(而上述的n:1也就是指n那方的类中存在1那方的对象(1个)作为属性)
方法一:annotation
1那方的类不需要设置什么,只需要在n那方加入@ManyToOne的注解和说明外键的字段名(此处如果不说明则会创建3张表,其中一张为中间关系表)
@Entity
@Table(name="_group")
public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@OneToMany
@JoinColumn(name="groupId")
//由于数据库表设计时,总是在多的那方加外键,所以name指的还是应该是User表
//的外键名称groupId。
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法二:xml配置
l 在n那方的类中加入1那方类对象的作为属性(多个);
l 在n方法的类.hbm.xml中加入配置标签:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Group" table="t_group">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.yilong.hibernate.model.User"/>
</set>
</class>
</hibernate-mapping>
(5) 1:n和n:1双向关联
实质就是上述(3)(4)的合并,首先都要在自己方的类中加入对方的类作为属性。
方法一:annotation
在1那方对应的类中添加注解OneToMany,并设置mappedBy属性:
@Entity
@Table(name="_group")
public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@OneToMany(mappedBy="group")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在n那方添加注解ManyToOne:
@Entity
@Table(name="_user")
public class User {
private int id;
private String name;
private Group group;
@ManyToOne
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法二:xml配置
Group.hbm.xml配置:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Group" table="t_group">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.yilong.hibernate.model.User"/>
</set>
</class>
</hibernate-mapping>
User.hbm.xml配置:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
</hibernate-mapping>
说明:Group.hbm.xml文件中的<key column="groupId"></key>中的column属性要与User.hbm.xml文件中<many-to-one name="group" column="groupId"></many-to-one>的column属性一样,这样才不会产生多余的外键字段。
(6) n:n 单向关联
n:n单向关联指的是其中n那方的类拥有对方的实例作为属性,下面还是以”教师-学生”模式为例。现规定教师能够知道所教导的所有学生的情况,而学生却不知道教师的情况。
方法一:annotation
l 在教师Teacher类的那方加入学生(多个--set)Student类作为自身的属性;
l 加入@ManyToMany注解,并为了使得创建的表名和字段名规范,还得使用注解@JoinTable。
@Entity
@Table(name="_teacher")
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@ManyToMany
@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="teacher_id")},
inverseJoinColumns={@JoinColumn(name="student_id")}
)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法二:xml配置
Student.hbm.xml中不需要添加配置:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
Teacher.hibernate.xml配置如下:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.yilong.hibernate.model.Student"
column="student_id"/>
</set>
</class>
</hibernate-mapping>
(7) n:n 双向关联
双向关联:双方都知道如何找到对方(比较少用)
方法一:annotation
只需要在n:n单向关联基础上对另外一方的类中加入注解@ManyToMany即可:
Student.java文件
@Entity
@Table(name="_student")
public class Student {
private int id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToMany(mappedBy="students")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法二:xml配置
在两个类.hbm.xml配置上都进行n:n单向配置时的配置即可:
Student.hbm.xml文件:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="teachers" table="t_s">
<key column="student_id"></key>
<many-to-many class="com.yilong.hibernate.model.Teacher"
column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
Teacher.hbm.xml文件:
<hibernate-mapping package="com.yilong.hibernate.model">
<class name="Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.yilong.hibernate.model.Student"
column="student_id"/>
</set>
</class>
</hibernate-mapping>