hibernate框架学习之多表查询helloworld
Posted 架构师小跟班
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate框架学习之多表查询helloworld相关的知识,希望对你有一定的参考价值。
package cn.itcast.h3.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import cn.itcast.h3.hql.vo.StudentModel; import cn.itcast.h3.hql.vo.TeacherModel; import cn.itcast.h3.util.HibernateUtil; //多表查询 public class MulHQLApp { //内连接 void testMulInner(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel tm inner join tm.students s"); List<Object[]> queryList = q.list(); System.out.println(queryList.size()); for(Object[] objs:queryList){ for(Object obj:objs){ System.out.print(obj+" "); } System.out.println(); } s.close(); } //迫切内连接 void testMulInner2(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("select DISTINCT tm from TeacherModel tm inner join fetch tm.students s"); List<Object> queryList = q.list(); System.out.println(queryList.size()); for(Object obj:queryList){ System.out.println(obj); } s.close(); } //隐式内连接 void testMulInner3(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<Object> queryList = q.list(); System.out.println(queryList.size()); for(Object obj:queryList){ System.out.println(obj); } s.close(); } //左外连接 void testMulOuter1(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel tm left outer join tm.students s"); List<Object[]> queryList = q.list(); System.out.println(queryList.size()); for(Object[] objs:queryList){ for(Object obj:objs){ System.out.print(obj+" "); } System.out.println(); } s.close(); } //迫切左外连接 void testMulOuter2(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel tm left outer join fetch tm.students s"); List<Object> queryList = q.list(); System.out.println(queryList.size()); for(Object obj:queryList){ System.out.println(obj); } s.close(); } //右外连接 void testMulOuter3(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel tm right outer join tm.students s"); List<Object[]> queryList = q.list(); for(Object[] objs:queryList){ for(Object obj:objs){ System.out.print(obj+" "); } System.out.println(); } s.close(); } void fn(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); // System.out.println(((TeacherModel)queryList.get(0)[0]).getStudents()); // System.out.println(((StudentModel)queryList.get(0)[1]).getTeacher()); System.out.println(queryList.get(0).getStudents()); s.close(); } void fn1(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel tm right outer join tm.students s"); List<Object[]> queryList = q.list(); System.out.println(((TeacherModel)queryList.get(35)[0]).getStudents()); // System.out.println(((StudentModel)queryList.get(0)[1]).getTeacher()); // System.out.println(queryList.get(30).getStudents().size()); s.close(); } public static void main(String[] args) { new MulHQLApp().fn1(); } } /* List<Object> queryList = q.list(); for(Object obj:queryList){ System.out.println(obj); } List<Object[]> queryList = q.list(); for(Object[] objs:queryList){ for(Object obj:objs){ System.out.print(obj+" "); } System.out.println(); } */
以上是关于hibernate框架学习之多表查询helloworld的主要内容,如果未能解决你的问题,请参考以下文章
hibernate框架学习之数据查询(QBC)helloworld