1. 环境,student对lesson,多对多的关系,关系表格:student_lesson(student_id,lesson_id)
2. 配置文件
1) student设为关联维护端
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<
set
name
="lessons"
table
="student_lesson"
inverse
=”false”>
<key column
="student_id"
/>
<
many-to-many
class
="Lesson"
column
="lesson_id"
/>
</
set
>
2) lesson设为逆向关联端
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<
set
name
="students"
table
="student_lesson"
inverse
="true"
>
<
key
column
="lesson_id"
/>
<
many-to-many
class
="Student"
column
="student_id"
></
many-to-many
>
</
set
>
3. 推荐设置
一般的将教为主动的一端,配置为关联正向端,本例中学生比课程更为主动,一般不使用 cascade=”true”选项,因为课程不以学生而存在,增加学生时候不会处理课程资料只处理学生和课程的关系,因此 inverse=”false”,cascade=”true”,在增加学生的时候,必须选择库存中的课程而后在存储到学生中
在对多多的关联中无法设置 CASCADE
属性
学生配置:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<
set
name
="lessons"
table
="student_lesson"
inverse
="false"
>
<
key
column
="student_id"
/>
<
many-to-many
class
="Lesson"
column
="lesson_id"
/>
</
set
>
期望结果,如果删除学生,参与的课程的资料不删除,与课程的参与关系将自动删除,这个起到与级连相似的结果,同样,选修和退修课程在学生端操作
4. 新增学生
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Student student
=
new
Student();
student.setStudentId(
"
007
"
);
student.setName(
"
ale
"
);
session.saveOrUpdate(student);
5. 新增课程
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Lesson lesson
=
new
Lesson();
lesson.setLessonId(
"
20070103
"
);
lesson.setLessonName(
"
激光
"
);
session.saveOrUpdate(lesson
6. 学生选修课程
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Lesson lesson
=
null
;
Student student
=
(Student)session.load(Student.
class
,
"
005
"
);
lesson
=
(Lesson)session.load(Lesson.
class
,
"
20070101
"
);
student.getLessons().add(lesson);
lesson
=
(Lesson)session.load(Lesson.
class
,
"
20070103
"
);
student.getLessons().add(lesson);
7. 学生退出课程选修
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Lesson lesson
=
null
;
Student student
=
(Student)session.load(Student.
class
,
"
005
"
);
lesson
=
(Lesson)session.load(Lesson.
class
,
"
20070101
"
);
student.getLessons().add(lesson);
lesson
=
(Lesson)session.load(Lesson.
class
,
"
20070103
"
);
student.getLessons().remove(lesson);
session.saveOrUpdate(student);
8. 取消课程
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Lesson lesson
=
(Lesson)session.load(Lesson.
class
,
"
20070103
"
);
session.delete(lesson);
由于配置了学生和课程双向维护关系,删除课程时,也删除了学生与课程的关系
9. 删除学生资料
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
Student student
=
(Student)session.load(Student.
class
,
"
005
"
);
session.delete(student);
将级连删除与课程的关系