首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibenrate之多对多联系关系

2012-08-26 
Hibenrate之多对多关联Hibernate多对多关联可以分成单向和双向,多对多关系会借助中间表来保存映射信息,所

Hibenrate之多对多关联

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 /><set name="tschool" table="stu_school_link" cascade="all"><key column="stuId"/><many-to-many 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></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 /><set name="stus" table="stu_school_link" cascade="save-update"><key column="schoolId"/><many-to-many column="stuId" name="code">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 /><set name="tschool" table="stu_school_link" cascade="save-update"><!-- "stuId":连接表中关联当前实体类的列名 --><key column="stuId"/><!-- "schoolId":是连接表中关联本实体类的外键 --><many-to-many column="schoolId"/></set></class></hibernate-mapping>

?附上测试类;

?

热点排行