欢迎来到天天文库
浏览记录
ID:18462565
大小:46.00 KB
页数:14页
时间:2018-09-18
《hibernate表间关联》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、hibernate表间关联多对一单向关联(学校和学生)school.javapublicclassSchool{privateintid;privateStringschoolName;privateStringaddress;.....对应属性的getter、setter方法}school.hbm.xml2、"native">student.javapublicclassStudent{privateintid;privateStringname;privateSchoolschool;.....对应属性的getter、setter方法}stdent.hbm.xml3、ate-mapping>测试方法p4、ublicvoidtestManyToOne(){Configurationcfg=(newConfiguration()).configure("/hibernate.cfg.xml");SessionFactorysf=cfg.buildSessionFactory();Sessionssn=sf.openSession();Transactiont=ssn.beginTransaction();Schoolschool=newSchool();school.setAddress("sdfsdfg");5、school.setSchoolName("werfet");Studentstudent1=newStudent();student1.setName("zhangjiang");student1.setSchool(school);Studentstudent2=newStudent();student2.setName("wangli");student2.setSchool(school);ssn.save(school);ssn.save(student1);ssn.save(student2);6、t.commit();Studentstu=(Student)ssn.load(Student.class,1);System.out.print(stu.getName());}多对一双向关联(学校和学生)school.javapublicclassSchool{privateintid;privateStringschoolName;privateStringaddress;privateSetstudents;.....对应属性的getter、setter方法}school.hbm.7、xml<8、keycolumn="schoolid">student.javapublicclassStudent{privateintid;privateStringname;privateSchoolschool;.....对应属性的getter、setter方法}std
2、"native">
3、ate-mapping>测试方法p
4、ublicvoidtestManyToOne(){Configurationcfg=(newConfiguration()).configure("/hibernate.cfg.xml");SessionFactorysf=cfg.buildSessionFactory();Sessionssn=sf.openSession();Transactiont=ssn.beginTransaction();Schoolschool=newSchool();school.setAddress("sdfsdfg");
5、school.setSchoolName("werfet");Studentstudent1=newStudent();student1.setName("zhangjiang");student1.setSchool(school);Studentstudent2=newStudent();student2.setName("wangli");student2.setSchool(school);ssn.save(school);ssn.save(student1);ssn.save(student2);
6、t.commit();Studentstu=(Student)ssn.load(Student.class,1);System.out.print(stu.getName());}多对一双向关联(学校和学生)school.javapublicclassSchool{privateintid;privateStringschoolName;privateStringaddress;privateSetstudents;.....对应属性的getter、setter方法}school.hbm.
7、xml<
8、keycolumn="schoolid">student.javapublicclassStudent{privateintid;privateStringname;privateSchoolschool;.....对应属性的getter、setter方法}std
此文档下载收益归作者所有