HQL多表查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HQL多表查询相关的知识,希望对你有一定的参考价值。

参考技术A SQL语句:select * from t_user t, linkman l where t.name= l.name;

hql语句:from t_user t inner join t.setLinkMan

Query query = session.createQuery("from t_user t inner join t.setLinkMan);

List list = query.list();//返回list ,list里面每部分是数组形式

(1)迫切内连接和内连接底层实现一样

(2)区别:使用内连接返回list每部分是数组,使用迫切内连接返回list每部分是对象

HQL语句:from t_user t inner join fetch c.setLinkMan

SQL语句:select * from t_user t left outer join linkman l on t.name=l.name

HQL语句:from t_user t left outer join t.setLinkMan

HQL语句:from t_user t left outer join fetch t.setLinkMan

注意:左外连接返回list每部分是数组,迫切左外连接list每部分是对象

SQL语句:select * from t_user t right outer join linkman l on t.name=l.name

HQL语句:from t_user t right outer join  t.setLinkMan

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();
        }


*/

 

以上是关于HQL多表查询的主要内容,如果未能解决你的问题,请参考以下文章

HQL多表查询 ---- Hibernate之查询语句

HQL多表查询

hql 多表查询

HibernateHibernate的多表查询

Hibernate的HQL多表查询

hql多表查询转换问题,小弟受教了