Hibernate多对多关联可以分成单向和双向,多对多关系会借助中间表来保存映射信息,所以一般少用;
- 单向
实例:学生可以在多个学校就读过,多个学校也会有多个学生;
TStudent:
public class TStudent implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer stuId;
private String name;
private Set<TSchool> tschool;
get()/set()
TStudent.hbm.xml:
<hibernate-mapping package="com.keith.many2many">
<class name="TStudent" table="TStudent">
<id name="stuId">
<generator class="native"/>
</id>
<property name="name" />
<set name="tschool" table="stu_school_link" cascade="all">
<key column="stuId"/>
<many-to-many class="TSchool" column="schoolId"/>
</set>
</class>
</hibernate-mapping>
TSchool:
public class TSchool implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer schoolId;
private String name;
get()/set()
}
TSchool.hbm.xml:
<hibernate-mapping package="com.keith.many2many"> <class name="TSchool" table="TSchool"> <id name="schoolId"> <generator class="native"/> </id> <property name="name" /> </class> </hibernate-mapping>
测试:
//添加数据
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
TStudent student = new TStudent();
student.setName("柯");
Set<TSchool> schools = new HashSet<TSchool>();
TSchool school = new TSchool();
school.setName("小学");
TSchool school1 = new TSchool();
school1.setName("中学");
TSchool school2 = new TSchool();
school2.setName("大学");
schools.add(school);
schools.add(school1);
schools.add(school2);
student.setTschool(schools);
session.save(student);
session.getTransaction().commit();
//查询数据
TStudent stu = (TStudent) session.load(TStudent.class, 1);
System.out.println("student's Name:" + stu.getName());
Iterator<TSchool> it = stu.getTschool().iterator();
TSchool school = null;
while (it.hasNext()) {
school = it.next();
System.out.println("student's school Name"+school.getName());
}
- 双向,在TSchool一方加入TStudent对象;
TSchool:
public class TSchool implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer schoolId;
private String name;
private Set<TStudent> stus = new HashSet();
get()/set()
}
TSchool.hbm.xml:
<hibernate-mapping package="com.keith.many2many.doubleSide">
<class name="TSchool" table="TSchool">
<id name="schoolId">
<generator class="native"/>
</id>
<property name="name" />
<set name="stus" table="stu_school_link" cascade="save-update">
<key column="schoolId"/>
<many-to-many column="stuId" class="TStudent"/>
</set>
</class>
</hibernate-mapping>
TStudent:
public class TStudent implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer stuId;
private String name;
private Set<TSchool> tschool;
get()/set();
}
TStudent.hbm.xml:
<hibernate-mapping package="com.keith.many2many.doubleSide"> <class name="TStudent" table="TStudent"> <id name="stuId"> <generator class="native"/> </id> <property name="name" /> <set name="tschool" table="stu_school_link" cascade="save-update"> <!-- "stuId":连接表中关联当前实体类的列名 --> <key column="stuId"/> <!-- "schoolId":是连接表中关联本实体类的外键 --> <many-to-many class="TSchool" column="schoolId"/> </set> </class> </hibernate-mapping>
附上测试类;