欢迎来到天天文库
浏览记录
ID:34723254
大小:88.68 KB
页数:8页
时间:2019-03-10
《hibernate对象间关系映射从入门到精通(多对多关系)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、主题:Hibernate对象间关系映射从入门到精通(多对多关系)相关文章:父子关系及inverse详解请问:Session被关闭了,为什么还能用?cascadeinverse推荐圈子:osworkflow更多相关推荐DescribeHibernate对象间关系映射从入门到精通(多对多关系)here.下面来谈一下多对多的关系:以老师和学生的关系为例子:<classname="com.etech.bm.po.TeacherPO"table="TEACHER"><idname="id"column="ID"type="integer">&l
2、t;generatorclass="identity"/></id><bagname="student"table="TEACHERSTUDENT"lazy="true"cascade="all"><keycolumn="TEACHERID"/><many-to-manyclass="com.etech.bm.po.StudentPO"column="STUDENTID"/></bag></class><classname="com.etech.bm.po.StudentPO
3、"table="STUDENT"><idname="id"column="ID"type="integer"><generatorclass="identity"/></id><bagname="teacher"table="TEACHERSTUDENT"lazy="true"cascade="all><keycolumn="STUDENTID"/><many-to-manyclass="com.etech.bm.po.TeacherPO"column="TEACHERID"/>&
4、lt;/bag></class>由工具自动生成的dll为droptableTEACHERSTUDENT;droptableTEACHER;droptableSTUDENT;createtableTEACHERSTUDENT(TEACHERIDINTEGERnotnull,STUDENTIDINTEGERnotnull,primarykey(TEACHERID,STUDENTID));createtableTEACHER(IDINTEGERnotnullgeneratedbydefaultasidentity,primarykey(ID));
5、createtableSTUDENT(IDINTEGERnotnullgeneratedbydefaultasidentity,primarykey(ID));altertableTEACHERSTUDENTaddconstraintFKFE10D819307EFC76foreignkey(STUDENTID)referencesSTUDENT;altertableTEACHERSTUDENTaddconstraintFKFE10D8198B06785Dforeignkey(TEACHERID)referencesTEACHER;程序ITxMgrtx=nul
6、l;tx=HibernateTxMgr.beginTrans("Addanewrelationships...");session=(Session)tx.getSession();student=newStudentPO();student1=newStudentPO();Liststudentlist=newArrayList();studentlist.add(student);studentlist.add(student1);teacher=newTeacherPO();teacher.setStudent(studentlist);session
7、.save(teacher);session.flush();tx.endTrans();生成的sqlHibernate:insertintoTEACHER(ID)values(default)Hibernate:insertintoSTUDENT(ID)values(default)Hibernate:insertintoSTUDENT(ID)values(default)Hibernate:insertintoTEACHERSTUDENT(TEACHERID,STUDENTID)values(?,?)结果C:MyappSQLLIBBIN>db
8、2select*fromstudentID-----
此文档下载收益归作者所有