Hibernate框架中如何将多对多映射转化为多对一映射

      在hibernate映射中,多对多映射是最麻烦,最复杂的映射,通常情况下我们都将其转化为多对一映射来提高效率。

      本文以老师与课程的对应关系(一个老师可以交多门课程,一门课程可以由多个老师任教)为案例讨论转化的实现:重点考虑其映射文件的配置。

      显然以上关系,通过一个中间表(teacher_course),我们可以将其简化成两个多对一得关系:

      teacher_course 与teacher 多对一;

      teacher_course 与course 多对一;

      建立Teacher.java 文件

 package cn.csu.domain; /** * @file Teacher.java * @version 1.0 * @since Mar 14, 2010 */ public class Teacher { private int id; private String teacherName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } }

Teacher.hbm.xml 文件

建立Course.java文件

 package cn.csu.domain; /** * @file Course.java * @version 1.0 * @since Mar 14, 2010 */ public class Course { private int id; private String courseName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } }

相应Course.hbm.xml

 

接下来建立一个中间类,取代多对多映射

建立TeacherCourse.java 文件

package cn.csu.domain; /**中间类 * @file TeacherCourse.java * @version 1.0 * @since Mar 14, 2010 */ public class TeacherCourse { private int id; private Teacher teacher; private Course course; public int getId() { return id; } public void setId(int id) { this.id = id; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }

相应TeacherCourse.hbm.xml

至此,三者之间的映射关系就建立完成。

可用如下测试类,测试:

建立Many2Many.java文件

package cn.csu.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import cn.csu.domain.Course; import cn.csu.domain.Teacher; import cn.csu.domain.TeacherCourse; import cn.csu.util.HibernateUtil; /**多对多关系转化成三个表进行关系映射 * @file Many2Many.java * @version 1.0 * @since Mar 14, 2010 */ public class Many2Many { public static void main(String[] args) { create(); get(); } //多对多关系的插入 static void create(){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); Teacher t1=new Teacher(); t1.setTeacherName("t1"); Teacher t2=new Teacher(); t2.setTeacherName("t2"); Course c1=new Course(); c1.setCourseName("c1"); Course c2=new Course(); c2.setCourseName("c2"); TeacherCourse tc1=new TeacherCourse(); tc1.setTeacher(t1); tc1.setCourse(c1); TeacherCourse tc2=new TeacherCourse(); tc2.setTeacher(t1); tc2.setCourse(c2); TeacherCourse tc3=new TeacherCourse(); tc3.setTeacher(t2); tc3.setCourse(c1); TeacherCourse tc4=new TeacherCourse(); tc4.setTeacher(t2); tc4.setCourse(c2); s.save(t1); s.save(t2); s.save(c1); s.save(c2); s.save(tc1); s.save(tc2); s.save(tc3); tx.commit(); System.out.println("operate succeed!!!"); }catch(Exception e){ e.printStackTrace(); if(tx!=null){ tx.rollback(); } }finally{ if(s!=null){ s.close(); } } } //多对多关系的查询, static void get(){ Session s=null; try{ s=HibernateUtil.getSession(); //先查出对应姓名的老师的id号 String hql1="from Teacher where teacherName=:teacherName"; Query query1=s.createQuery(hql1); query1.setString("teacherName", "t1"); List list1=query1.list(); Teacher teacher=list1.get(0); System.out.println("Teacher id:"+teacher.getId()); //以该id号查出中间表中相应课程号,从而获得课程名 String hql2="from TeacherCourse where teacher=:teacher"; Query query2=s.createQuery(hql2); query2.setEntity("teacher", teacher); List list2=query2.list(); for(TeacherCourse m:list2){ System.out.println("courseName="+ m.getCourse().getCourseName()); } }catch(Exception e){ e.printStackTrace(); }finally{ if(s!=null){ s.close(); } } } }

 

你可能感兴趣的:(Java,hibernate,框架,class,string,generator,query)