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+"\t"); } 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+"\t"); } 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+"\t"); } 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+"\t"); } System.out.println(); } */
--------------------迷人的分割线--------------------
作者:架构师小跟班
开发者导航: http://www.codernav.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出,以免更多的人被误导。
作者:架构师小跟班
开发者导航: http://www.codernav.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出,以免更多的人被误导。