多对多关系(学生Student,课程Course)

学生类的定义以及hbm文件的配置如下

1 public class Student {
2     private int id;
3     private String name;
4     private Set<Course> courses = new HashSet<Course>();
5 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6     <class name="com.zlt.hibernatedemo.Student" table="student">
 7         <id name="id" column="id">
 8             <generator class="increment"></generator>
 9         </id>
10         
11         <property name="name" type="java.lang.String">  
12             <column name="name" length="50" />  
13         </property>
14         
15         <set name="courses" table="score" cascade="all">
16             <key column="studentid"></key>
17             <many-to-many class="com.zlt.hibernatedemo.Course" column="courseid"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

课程类的定义以及hbm文件的配置如下

1 public class Course {
2     private int id;
3     private String courseName;
4     private Set<Student> students = new HashSet<Student>();
5 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6     <class name="com.zlt.hibernatedemo.Course" table="course">
 7         <id name="id" column="id">
 8             <generator class="increment"></generator>
 9         </id>
10         
11         <property name="courseName"></property>
12         
13         <set name="students" table="score">
14             <key column="courseid"></key>
15             <many-to-many class="com.zlt.hibernatedemo.Student" column="studentid"/>
16         </set>
17         
18     </class>
19 
20 </hibernate-mapping>

 

测试程序

 1 public class HibernateTest {
 2     public static void main(String[] args) {
 3         Session session = HibernateFactory.currentSession();
 4         Transaction tx = session.beginTransaction();
 5         
 6         
 7         Student student1 = new Student();
 8         student1.setName("student1");
 9         
10         Student student2 = new Student();
11         student2.setName("student2");
12         
13         Course course1 = new Course();
14         course1.setCourseName("course1");
15         
16         Course course2 = new Course();
17         course2.setCourseName("course2");
18         
19         //由于使用了默认的inverse(false),所以两端都可以维护关联关系
20         student1.getCourses().add(course1);
21         student1.getCourses().add(course2);
22         
23 
24         session.save(student1);
25         //student设置了级联,所以不需要额外的插入course
26 //        session.save(course1);
27 //        session.save(course2);
28         
29         tx.commit();
30         session.close();
31         
32     }
33 }

 

结果

 

如果多对多辅助表中还有其他的属性,就得把多对多关系拆分成两个一对多关系

学生Student

1 public class Student {
2     private int id;
3     private String name;
4     private Set<Score> courses = new HashSet<Score>();    
5 }

 

课程Course

1 public class Course {
2     private int id;
3     private String courseName;
4     private Set<Score> scores = new HashSet<Score>();
5 }

 

成绩Score

1 public class Score {
2     private int id;
3     private Student student;
4     private Course course;
5     
6     //额外的属性
7     private int score; 
8 }

 

 

posted on 2014-06-30 22:28  幸福小弥  阅读(175)  评论(0编辑  收藏  举报