本文主要是介绍Hibernate进阶之多对多双向关联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package cn.itcast.d_many22many;import java.util.Date;
import java.util.HashSet;
import java.util.Set;import javassist.expr.NewArray;/*** 项目* * @author Jie.Yuan* */
public class Student {private int id;private String name;private Set<Teacher> teachers=new HashSet<Teacher>();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;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", teachers="+ teachers + "]";}
}
package cn.itcast.d_many22many;import java.util.HashSet;
import java.util.Set;import javassist.expr.NewArray;/*** 开发人员* * @author Jie.Yuan* */
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;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + ", students="+ students + "]";}}
student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.d_many22many"><class name="Student" table="student"><id name="id"><generator class="native"></generator></id><property name="name" column="name"/><set name="teachers" table="teacher_student"><key column="student_id"></key><!-- 查找这个学生被那个老师教 --><many-to-many class="Teacher" column="teacher_id"></many-to-many></set></class></hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.d_many22many"><class name="Teacher" table="teacher"><id name="id" column="id"><generator class="native"></generator></id><property name="name" column="name" /><set name="students" table="teacher_student"><!--查老师的id的 和中间表联系--><key column="teacher_id"/><!-- 查学生的id和中间表的关系 --><many-to-many class="Student" column="student_id"></many-to-many></set></class>
</hibernate-mapping>
package cn.itcast.d_many22many;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;import org.hibernate.classic.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import cn.itcast.d_many22many.Student;
import cn.itcast.d_many22many.Teacher;public class App_many22many {//双向多对多private static SessionFactory sf;static {sf = new Configuration().configure().addClass(Student.class) .addClass(Teacher.class) // 测试时候使用.buildSessionFactory();}@Testpublic void add(){Session session = sf.openSession();session.beginTransaction();Teacher t1 = new Teacher();t1.setName("王老师");Teacher t2 = new Teacher();t2.setName("李老师");Student s1 = new Student();s1.setName("王同学");Student s2 = new Student();s2.setName("李同学");Set<Student> ss = new HashSet<Student>();ss.add(s1);ss.add(s2);t1.setStudents(ss);// 告诉Hibernate老师教了那两个学生t2.setStudents(ss);session.save(t1);session.save(t2);session.save(s1);session.save(s2);session.getTransaction().commit();session.close();}@Testpublic void query(){//双向关联体现在查找上Session session = sf.openSession();session.beginTransaction();Teacher teacher = (Teacher) session.get(Teacher.class, 2);System.out.println("教室的姓名:" + teacher.getName());System.out.println("教室的学生列表:");Iterator<Student> iterator = teacher.getStudents().iterator();while (iterator.hasNext()) {Student student = iterator.next();System.out.println(student.getName() + " ");}System.out.println("===========逆向==============");Student student = (Student) session.get(Student.class, 1);System.out.println("学生的姓名:" + student.getName());System.out.println("学生被教的老师:");Iterator<Teacher> iterator2 = student.getTeachers().iterator();while (iterator2.hasNext()) {Teacher teacher2 = iterator2.next();System.out.println(teacher2.getName() + " ");}System.out.println();session.getTransaction().commit();session.close();}
}
这篇关于Hibernate进阶之多对多双向关联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!