HIBERNATE多对多配置

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);
将级连删除与课程的关系
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值