码迷,mamicode.com
首页 > 系统相关 > 详细

hibernate关系映射(多对多)

时间:2014-07-01 00:41:37      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

多对多关系(学生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 }

 

结果

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

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

学生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 }

 

 

hibernate关系映射(多对多),布布扣,bubuko.com

hibernate关系映射(多对多)

标签:style   blog   http   java   color   使用   

原文地址:http://www.cnblogs.com/zanglitao/p/3817390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!