2022年hibernate多表查询 .pdf

上传人:C****o 文档编号:39729978 上传时间:2022-09-07 格式:PDF 页数:19 大小:162.47KB
返回 下载 相关 举报
2022年hibernate多表查询 .pdf_第1页
第1页 / 共19页
2022年hibernate多表查询 .pdf_第2页
第2页 / 共19页
点击查看更多>>
资源描述

《2022年hibernate多表查询 .pdf》由会员分享,可在线阅读,更多相关《2022年hibernate多表查询 .pdf(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、.多表查询:三个以上的表之间的关联;2.配置:分别配置两表之间的关联映射;3.查询:from a join b join c.join z hibernate.cfg.xml:view sourceprint?01.02.05.06.07.08.09.10.scott 11.12.jdbc:oracle:thin:127.0.0.1:1521:MGC 13.14.15.org.hibernate.dialect.Oracle9Dialect 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 19 页 -16.17.MGC 18.tiger 19.20.oracle.jdbc.dri

2、ver.OracleDriver 21.22.23.true 24.25.26.27.28.Student.java:view sourceprint?01.package cn.edu.ahau.mgc.hibernate.pojo;02.03.publicclass Student 04.05.privatelong id;06.privateString name;07.privateClasses classes;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 19 页 -08.09.publicClasses getClasses()10.returnclasses;1

3、1.12.13.publicvoid setClasses(Classes classes)14.this.classes=classes;15.16.17.publiclong getId()18.returnid;19.20.21.publicvoid setId(longid)22.this.id=id;23.24.25.publicString getName()26.returnname;27.28.29.publicvoid setName(String name)30.this.name=name;31.32.Classes.java:view source名师资料总结-精品资料

4、欢迎下载-名师精心整理-第 3 页,共 19 页 -print?01.package cn.edu.ahau.mgc.hibernate.pojo;02.03.importjava.util.Set;04.05.publicclass Classes 06.07.privatelong id;08.privateString name;09.privateSet students;10.11.publiclong getId()12.returnid;13.14.15.publicvoid setId(longid)16.this.id=id;17.18.19.publicString get

5、Name()20.returnname;21.22.23.publicvoid setName(String name)24.this.name=name;25.名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 19 页 -26.27.publicSet getStudents()28.returnstudents;29.30.31.publicvoid setStudents(Set students)32.this.students=students;33.34.Course.java:view sourceprint?01.package cn.edu.ahau.mgc.hi

6、bernate.pojo;02.03.publicclass Course 04.05.privatelong id;06.privateString name;07.08.publiclong getId()09.returnid;10.11.12.publicvoid setId(longid)名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 19 页 -13.this.id=id;14.15.16.publicString getName()17.returnname;18.19.20.publicvoid setName(String name)21.this.name=n

7、ame;22.23.Grade.java:view sourceprint?01.package cn.edu.ahau.mgc.hibernate.pojo;02.03.publicclass Grade 04.05.privatelong id;06.privateStudent student;07.privateCourse course;08.privateint grade;09.10.publiclong getId()名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 19 页 -11.returnid;12.13.14.publicvoid setId(longid

8、)15.this.id=id;16.17.18.publicStudent getStudent()19.returnstudent;20.21.22.publicvoid setStudent(Student student)23.this.student=student;24.25.26.publicCourse getCourse()27.returncourse;28.29.30.publicvoid setCourse(Course course)31.this.course=course;32.33.34.publicint getGrade()35.returngrade;36.

9、37.38.publicvoid setGrade(intgrade)39.this.grade=grade;名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 19 页 -40.41.Join.hbm.xml:view sourceprint?01.02.04.07.08.09.10.11.12.13.14.15.16.17.名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 19 页 -18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.名师资料总结-精品资料欢迎下载-名师

10、精心整理-第 9 页,共 19 页 -43.44.45.46.47.48.49.50.51.52.53.54.55.HibernateSessionFactory.java:view sourceprint?001.package cn.edu.ahau.mgc.hibernate.many2one.factory;002.003.importorg.hibernate.HibernateException;004.importorg.hibernate.Session;005.importorg.hibernate.cfg.Configuration;006.名师资料总结-精品资料欢迎下载-

11、名师精心整理-第 10 页,共 19 页 -007./*008.*Configures and provides access to Hibernate sessions,tied to the 009.*current thread of execution.Follows the Thread Local Session 010.*pattern,see link http:/hibernate.org/42.html;.011.*/012.publicclass HibernateSessionFactory 013.014./*015.*Location of hibernate.cf

12、g.xml file.016.*Location should be on the classpath as Hibernate uses 017.*#resourceAsStream style lookup for its configuration file.018.*The default classpath location of the hibernate config file is 019.*in the default package.Use#setConfigFile()to update 020.*the location of the configuration fil

13、e for the current session.021.*/022.privatestaticString CONFIG_FILE_LOCATION=/hibernate.cfg.xml;023.privatestaticfinalThreadLocal threadLocal=newThreadLocal();024.privatestaticConfiguration configuration=newConfiguration();025.privatestaticorg.hibernate.SessionFactory sessionFactory;026.privatestati

14、cString configFile=CONFIG_FILE_LOCATION;027.028.static 029.try 名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 19 页 -030.configuration.configure(configFile);031.sessionFactory=configuration.buildSessionFactory();032.catch(Exception e)033.System.err 034.println(%Error Creating SessionFactory%);035.e.printStackTrace(

15、);036.037.038.privateHibernateSessionFactory()039.040.041./*042.*Returns the ThreadLocal Session instance.Lazy initialize 043.*the SessionFactory if needed.044.*045.*return Session 046.*throws HibernateException 047.*/048.publicstaticSession getSession()throwsHibernateException 049.Session session=(

16、Session)threadLocal.get();050.051.if(session=null|!session.isOpen()052.if(sessionFactory=null)053.rebuildSessionFactory();054.055.session=(sessionFactory!=null)?sessionFactory.openSession()056.:null;名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 19 页 -057.threadLocal.set(session);058.059.060.returnsession;061.062.

17、063./*064.*Rebuild hibernate session factory 065.*066.*/067.publicstaticvoid rebuildSessionFactory()068.try 069.configuration.configure(configFile);070.sessionFactory=configuration.buildSessionFactory();071.catch(Exception e)072.System.err 073.println(%Error Creating SessionFactory%);074.e.printStac

18、kTrace();075.076.077.078./*079.*Close the single hibernate session instance.080.*081.*throws HibernateException 082.*/083.publicstaticvoid closeSession()throwsHibernateException 084.Session session=(Session)threadLocal.get();名师资料总结-精品资料欢迎下载-名师精心整理-第 13 页,共 19 页 -085.threadLocal.set(null);086.087.if(

19、session!=null)088.session.close();089.090.091.092./*093.*return session factory 094.*095.*/096.publicstaticorg.hibernate.SessionFactory getSessionFactory()097.returnsessionFactory;098.099.100./*101.*return session factory 102.*103.*session factory will be rebuilded in the next call 104.*/105.publics

20、taticvoid setConfigFile(String configFile)106.HibernateSessionFactory.configFile=configFile;107.sessionFactory=null;108.109.110./*111.*return hibernate configuration 112.*名师资料总结-精品资料欢迎下载-名师精心整理-第 14 页,共 19 页 -113.*/114.publicstaticConfiguration getConfiguration()115.returnconfiguration;116.117.118.E

21、xportToDBCreate.java:view sourceprint?01.package cn.edu.ahau.mgc.hibernate.export;02.03.importorg.hibernate.cfg.Configuration;04.importorg.hibernate.tool.hbm2ddl.SchemaExport;05.06.publicclass ExportToDBCreate 07.08.publicstaticvoid main(String args)09.Configuration cfg=newConfiguration().configure(

22、);10.SchemaExport export=newSchemaExport(cfg);11.export.create(true,true);12.13.14.名师资料总结-精品资料欢迎下载-名师精心整理-第 15 页,共 19 页 -InitData.java:view sourceprint?01.package cn.edu.ahau.mgc.hibernate.export;02.03.importjava.util.ArrayList;04.importjava.util.Iterator;05.importjava.util.List;06.importjava.util.R

23、andom;07.08.importorg.hibernate.Session;09.10.importcn.edu.ahau.mgc.hibernate.factory.HibernateSessionFactory;11.importcn.edu.ahau.mgc.hibernate.pojo.Classes;12.importcn.edu.ahau.mgc.hibernate.pojo.Course;13.importcn.edu.ahau.mgc.hibernate.pojo.Grade;14.importcn.edu.ahau.mgc.hibernate.pojo.Student;1

24、5.16.17.publicclass InitData 18.19.20.publicstaticvoid main(String args)21.Session session=HibernateSessionFactory.getSession();名师资料总结-精品资料欢迎下载-名师精心整理-第 16 页,共 19 页 -22.session.beginTransaction();23.24.List students=newArrayList();25.for(inti=0;i 3;i+)26.Classes classes=newClasses();27.classes.setNa

25、me(Class_+i);28.session.save(classes);29.30.for(intj=0;j 3;j+)31.Student student=newStudent();32.student.setName(Class_+i+-Student_+j);33.student.setClasses(classes);34.session.save(student);35.students.add(student);36.37.38.39.List courses=newArrayList();40.for(inti=0;i 5;i+)41.Course course=newCou

26、rse();42.course.setName(Course_+i);43.session.save(course);44.courses.add(course);45.46.47.for(Iterator iterator=students.iterator();iterator.hasNext();)48.Student student=iterator.next();名师资料总结-精品资料欢迎下载-名师精心整理-第 17 页,共 19 页 -49.for(Iterator iterator2=courses.iterator();iterator2.hasNext();)50.Cours

27、e course=iterator2.next();51.Random random=newRandom();52.Grade grade=newGrade();53.grade.setStudent(student);54.grade.setCourse(course);55.grade.setGrade(random.nextInt(100);56.session.save(grade);57.58.59.60.session.getTransaction().commit();61.HibernateSessionFactory.closeSession();62.63.64.TestJ

28、oin.java:view sourceprint?01.package cn.edu.ahau.mgc.hibernate.export;02.03.importjava.util.Iterator;04.名师资料总结-精品资料欢迎下载-名师精心整理-第 18 页,共 19 页 -05.importorg.hibernate.Session;06.07.importcn.edu.ahau.mgc.hibernate.factory.HibernateSessionFactory;08.09.publicclass TestJoin 10.11.publicstaticvoid main(St

29、ring args)12.Session session=null;13.try 14.session=HibernateSessionFactory.getSession();15.Iterator students=session.createQuery(select c.name,s.name,g.grade,cl.name from Grade g join g.student s join g.course c join s.classes cl where g.grade=(select max(grade)from Grade).iterate();16.if(students.

30、hasNext()17.Object student=students.next();18.System.out.println(Course Name:+student0);19.System.out.println(Student Name:+student1);20.System.out.println(Grade:+student2);21.System.out.println(Class Name:+student3);22.23.catch(Exception e)24.e.printStackTrace();25.finally 26.HibernateSessionFactory.closeSession();27.28.29.名师资料总结-精品资料欢迎下载-名师精心整理-第 19 页,共 19 页 -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁