标签:
在Hibernate在-many关系。它通常不使用。由于当数据库查询复杂度太高时。
我们在这里做的是学生和教师,学生可以有多个老师,教师可以有多个学生。
我们首先建立一个学生实体类:Student.java
package cn.itcast.hibernate.domain; import java.util.Set; public class Student { private int id; private String name; private Set<Teacher> teachers; 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; } public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } }我们定义了三个三个属性。各自是id、name和一个set集合
然后是老师实体类:Teacher.java
package cn.itcast.hibernate.domain; import java.util.Set; public class Teacher { private int id; private String name; private Set<Student> students; 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; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }我们仍然定义了三个实体类,id、name和一个set集合
然后,我们看下Stduent类的映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Student"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <set name="teachers" table="teacher_student"> <key column="student_id" /> <many-to-many class="Teacher" column="teacher_id" /> </set> </class> </hibernate-mapping>在这个文件里。我们定义了:id自己主动增长、name属性。另一个<set>标签。name属性是Student.java中的set集合的那个属性。然后我们在<key>标签定义外键是student_id,然后又定义了一个<many-to-many>标签,规定了多对多的关系。
下边是Teacher.java类的映射文件:
<?和上边的Stduent类的映射文件差点儿相同xml version="1.0"?
> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Teacher"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <set name="students" table="teacher_student"> <key column="teacher_id" /> <many-to-many class="Student" column="student_id"/> </set> </class> </hibernate-mapping>
然后,我们写一个測试类:Many2Many.java
package cn.itcast.hibernate; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itcast.hibernate.domain.Student; import cn.itcast.hibernate.domain.Teacher; public class Many2Many { public static void main(String[] args) { add(); query(1); } static void add(){ Session s = null; Transaction tx = null; try{ //定义了一个Teahcer的set集合 Set<Teacher> ts = new HashSet<Teacher>(); //定义了一个Student的set集合 Set<Student> ss = new HashSet<Student>(); //添加一个老师1 Teacher t1 = new Teacher(); t1.setName("t1 name"); ts.add(t1); //添加一个老师2 Teacher t2 = new Teacher(); t2.setName("t2 name"); ts.add(t2); //添加一个学生1 Student s1 = new Student(); s1.setName("s1"); ss.add(s1); //添加一个学生2 Student s2 = new Student(); s2.setName("s2"); ss.add(s2); //这里是设置两个teacher的set属性 t1.setStudents(ss); t2.setStudents(ss); /* * 这是是设置两个student的set属性。假设和上边的设置同一时候出现,则会抛出异常。由于多对多的关系在上边已经建立 * s1.setTeachers(ts); s2.setTeachers(ts); */ s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(t1); s.save(t2); s.save(s1); s.save(s2); tx.commit(); }finally{ if(s!=null){ s.close(); } } } static void query(int id){ Session s = null; Transaction tx = null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); //依据id查询得到Teacher对象 Teacher t = (Teacher) s.get(Teacher.class, id); //把Teacher对象相应的学生的数量打出来 System.out.println("Students:"+t.getStudents().size()); tx.commit(); }finally{ if(s!=null){ s.close(); } } } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
【SSH三个框架】Hibernate第八部分基础:经营-many关系
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4617153.html