Hibernate HQL的子查询

时间:2022-09-17 14:56:04

子查询是SQL中很重要的功能,他可以在SQL中利用另外一条SQL的查询结果,HQL同样支持此机制

如果子查询返回都条记录,可以用以下关键字进行量化

all: 表示所有记
any:表示所有记录中的任意1条
some:与any用法一样
in:与=any等价
exist: 表示子查询至少要返回一条记录

数据库结构:

 

Hibernate HQL的子查询
Hibernate HQL的子查询
create   table  testStu (id  varchar ( 32 ),name  varchar ( 32 ),age  int ,team_id  varchar ( 32 ));
Hibernate HQL的子查询
Hibernate HQL的子查询
insert   into  teststu  values (" 1 ","tom1", 11 ," 1 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 2 ","tom2", 12 ," 2 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 3 ","tom3", 13 ," 1 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 4 ","tom4", 14 ," 2 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 5 ","tom5", 15 ," 1 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 6 ","tom6", 16 ," 2 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 7 ","tom7", 17 ," 1 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 8 ","tom8", 18 ," 2 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 9 ","tom9", 19 ," 1 ");
Hibernate HQL的子查询
insert   into  teststu  values (" 10 ","tom10", 20 ," 2 ");
Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询
create   table  testteam (id  varchar ( 32 primary   key ,name  varchar ( 32 ));
Hibernate HQL的子查询
Hibernate HQL的子查询
insert   into  testteam  values (" 1 ","team1");
Hibernate HQL的子查询
insert   into  testteam  values (" 2 ","team2");
Hibernate HQL的子查询
Hibernate HQL的子查询

 

POJO:

 

Hibernate HQL的子查询package  Search.filter;
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询
public   class  TestStu  ... {
Hibernate HQL的子查询    
private String id; //标识id
Hibernate HQL的子查询
    private String name; //学生姓名
Hibernate HQL的子查询
    private int age; //岁数
Hibernate HQL的子查询
    private TestTeam team;
Hibernate HQL的子查询  
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询     
public int getAge() ...{
Hibernate HQL的子查询        
return age;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public String getName() ...{
Hibernate HQL的子查询        
return name;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public void setId(String id) ...{
Hibernate HQL的子查询        
this.id = id;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public void setAge(int age) ...{
Hibernate HQL的子查询        
this.age = age;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public void setName(String stuName) ...{
Hibernate HQL的子查询        
this.name = stuName;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public String getId() ...{
Hibernate HQL的子查询        
return id;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public TestStu() ...//无参的构造函数
Hibernate HQL的子查询
    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public TestTeam getTeam() ...{
Hibernate HQL的子查询        
return team;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public void setTeam(TestTeam team) ...{
Hibernate HQL的子查询        
this.team = team;
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询}

Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询
package  Search.filter;
Hibernate HQL的子查询
Hibernate HQL的子查询
import  java.util.Set;
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询
public   class  TestTeam  ... {
Hibernate HQL的子查询   
private String id;
Hibernate HQL的子查询   
private String name;
Hibernate HQL的子查询   
private Set students;
Hibernate HQL的子查询Hibernate HQL的子查询
public String getId() ...{
Hibernate HQL的子查询    
return id;
Hibernate HQL的子查询}

Hibernate HQL的子查询Hibernate HQL的子查询
public void setId(String id) ...{
Hibernate HQL的子查询    
this.id = id;
Hibernate HQL的子查询}

Hibernate HQL的子查询Hibernate HQL的子查询
public String getName() ...{
Hibernate HQL的子查询    
return name;
Hibernate HQL的子查询}

Hibernate HQL的子查询Hibernate HQL的子查询
public void setName(String name) ...{
Hibernate HQL的子查询    
this.name = name;
Hibernate HQL的子查询}

Hibernate HQL的子查询Hibernate HQL的子查询
public Set getStudents() ...{
Hibernate HQL的子查询    
return students;
Hibernate HQL的子查询}

Hibernate HQL的子查询Hibernate HQL的子查询
public void setStudents(Set students) ...{
Hibernate HQL的子查询    
this.students = students;
Hibernate HQL的子查询}

Hibernate HQL的子查询}

Hibernate HQL的子查询

 

Hibernate.cfg.xml

 

Hibernate HQL的子查询<? xml version='1.0' encoding='UTF-8' ?>
Hibernate HQL的子查询
<! DOCTYPE hibernate-configuration PUBLIC
Hibernate HQL的子查询          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
Hibernate HQL的子查询          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
Hibernate HQL的子查询
Hibernate HQL的子查询
<!--  Generated by MyEclipse Hibernate Tools.                    -->
Hibernate HQL的子查询
< hibernate-configuration >
Hibernate HQL的子查询
Hibernate HQL的子查询
< session-factory >
Hibernate HQL的子查询    
< property  name ="connection.username" > root </ property >
Hibernate HQL的子查询    
< property  name ="connection.url" >
Hibernate HQL的子查询        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp; useUnicode=true
Hibernate HQL的子查询    
</ property >
Hibernate HQL的子查询    
< property  name ="dialect" >
Hibernate HQL的子查询        org.hibernate.dialect.MySQLDialect
Hibernate HQL的子查询    
</ property >
Hibernate HQL的子查询    
< property  name ="myeclipse.connection.profile" > mysql </ property >
Hibernate HQL的子查询    
< property  name ="connection.password" > 1234 </ property >
Hibernate HQL的子查询    
< property  name ="connection.driver_class" >
Hibernate HQL的子查询        com.mysql.jdbc.Driver
Hibernate HQL的子查询    
</ property >
Hibernate HQL的子查询    
< property  name ="hibernate.dialect" >
Hibernate HQL的子查询        org.hibernate.dialect.MySQLDialect
Hibernate HQL的子查询    
</ property >
Hibernate HQL的子查询    
< property  name ="hibernate.show_sql" > true </ property >
Hibernate HQL的子查询    
< property  name ="current_session_context_class" > thread </ property >
Hibernate HQL的子查询    
< mapping  resource ="Search/sub/TestStu.hbm.xml"   />
Hibernate HQL的子查询    
< mapping  resource ="Search/sub/TestTeam.hbm.xml"   />
Hibernate HQL的子查询
Hibernate HQL的子查询
</ session-factory >
Hibernate HQL的子查询
Hibernate HQL的子查询
</ hibernate-configuration >

 

TestStu.hbm.xml

 

Hibernate HQL的子查询<? xml version="1.0" encoding="utf-8" ?>
Hibernate HQL的子查询
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
Hibernate HQL的子查询"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
Hibernate HQL的子查询
<!--  
Hibernate HQL的子查询    Mapping file autogenerated by MyEclipse - Hibernate Tools
Hibernate HQL的子查询
-->
Hibernate HQL的子查询
< hibernate-mapping  package ="Search.fetch"   >
Hibernate HQL的子查询   
Hibernate HQL的子查询    
< class  name ="Search.sub.TestStu"  table ="teststu"  lazy ="true" >
Hibernate HQL的子查询       
< id  name ="id"  column ="id"  unsaved-value ="null" >
Hibernate HQL的子查询         
< generator  class ="uuid.hex" ></ generator >
Hibernate HQL的子查询       
</ id >
Hibernate HQL的子查询
Hibernate HQL的子查询       
< property  name ="name"  column ="name" ></ property >
Hibernate HQL的子查询       
< property  name ="age"  column ="age" ></ property >
Hibernate HQL的子查询       
< many-to-one  name ="team"  column ="team_id" >
Hibernate HQL的子查询       
</ many-to-one >
Hibernate HQL的子查询      
</ class >
Hibernate HQL的子查询
</ hibernate-mapping >
Hibernate HQL的子查询


TestTeam.hbm.xml

 

Hibernate HQL的子查询<? xml version="1.0" encoding="utf-8" ?>
Hibernate HQL的子查询
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
Hibernate HQL的子查询"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
Hibernate HQL的子查询
<!--  
Hibernate HQL的子查询    Mapping file autogenerated by MyEclipse - Hibernate Tools
Hibernate HQL的子查询
-->
Hibernate HQL的子查询
< hibernate-mapping >
Hibernate HQL的子查询
< class  name ="Search.sub.TestTeam"  table ="testteam"  lazy ="true" >
Hibernate HQL的子查询    
< id  name ="id"  column ="id"  unsaved-value ="null" >
Hibernate HQL的子查询        
< generator  class ="uuid.hex" ></ generator >
Hibernate HQL的子查询    
</ id >
Hibernate HQL的子查询    
< property  name ="name"  column ="name"  type ="string" ></ property >
Hibernate HQL的子查询    
< set  name ="students"  inverse ="true"  lazy ="true"  fetch ="select" >
Hibernate HQL的子查询      
< key  column ="team_id" ></ key >
Hibernate HQL的子查询      
< one-to-many  class ="Search.sub.TestStu" />
Hibernate HQL的子查询
Hibernate HQL的子查询    
</ set >
Hibernate HQL的子查询 
</ class >    
Hibernate HQL的子查询
Hibernate HQL的子查询
</ hibernate-mapping >
Hibernate HQL的子查询

 

测试代码:

 

Hibernate HQL的子查询package  Search.sub;
Hibernate HQL的子查询
Hibernate HQL的子查询
import  java.io.File;
Hibernate HQL的子查询
import  java.util.Iterator;
Hibernate HQL的子查询
import  java.util.List;
Hibernate HQL的子查询
Hibernate HQL的子查询
import  org.hibernate.Query;
Hibernate HQL的子查询
import  org.hibernate.Session;
Hibernate HQL的子查询
import  org.hibernate.SessionFactory;
Hibernate HQL的子查询
import  org.hibernate.Transaction;
Hibernate HQL的子查询
import  org.hibernate.cfg.Configuration;
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询
public   class  Test  ... {
Hibernate HQL的子查询
Hibernate HQL的子查询
Hibernate HQL的子查询Hibernate HQL的子查询    
public static void main(String[] args)...{
Hibernate HQL的子查询        String filePath
=System.getProperty("user.dir")+File.separator+"src/Search/sub"+File.separator+"hibernate.cfg.xml";
Hibernate HQL的子查询        File file
=new File(filePath);
Hibernate HQL的子查询        SessionFactory sessionFactory
=new Configuration().configure(file).buildSessionFactory();
Hibernate HQL的子查询        Session session
=sessionFactory.openSession();
Hibernate HQL的子查询        Transaction t
=(Transaction)session.beginTransaction();
Hibernate HQL的子查询        
Hibernate HQL的子查询        
//查询所有学生年龄都大于11岁的班级对象(结果应为team2)
Hibernate HQL的子查询        
//team1学生年龄是11,13,15,17,19
Hibernate HQL的子查询        
//team2学生年龄是12,14,16,17,20
Hibernate HQL的子查询Hibernate HQL的子查询
        /**//*
Hibernate HQL的子查询         * 使用SQL
Hibernate HQL的子查询         * select t1.name
Hibernate HQL的子查询           from testteam t1
Hibernate HQL的子查询           where 11<all(SELECT s.age from teststu s where s.team_id=t1.id);
Hibernate HQL的子查询         
*/

Hibernate HQL的子查询        Query query
=session.createQuery("from TestTeam t where 11<all(select s.age from t.students s)");
Hibernate HQL的子查询        
//注意:不能将11<all 写成all>11以违反Mysql规则
Hibernate HQL的子查询
        List teamList=query.list();
Hibernate HQL的子查询Hibernate HQL的子查询        
for (Iterator iterator = teamList.iterator(); iterator.hasNext();) ...{
Hibernate HQL的子查询            TestTeam object 
= (TestTeam) iterator.next();
Hibernate HQL的子查询            System.out.println(object.getName());
Hibernate HQL的子查询        }

Hibernate HQL的子查询        
Hibernate HQL的子查询        
//查询有一个学生的年龄为15岁的team对象(结果应为team1)
Hibernate HQL的子查询Hibernate HQL的子查询
        /**//*
Hibernate HQL的子查询         * 使用SQL
Hibernate HQL的子查询         * select t1.name
Hibernate HQL的子查询           from testteam t1
Hibernate HQL的子查询           where 15=any(SELECT s.age from teststu s where s.team_id=t1.id);
Hibernate HQL的子查询         
*/

Hibernate HQL的子查询        Query query1
=session.createQuery("from TestTeam t where 15=any(select s.age from t.students s)");
Hibernate HQL的子查询        List teamList1
=query1.list();
Hibernate HQL的子查询Hibernate HQL的子查询        
for (Iterator iterator = teamList1.iterator(); iterator.hasNext();) ...{
Hibernate HQL的子查询            TestTeam object1 
= (TestTeam) iterator.next();
Hibernate HQL的子查询            System.out.println(object1.getName());
Hibernate HQL的子查询        }

Hibernate HQL的子查询        t.commit();
Hibernate HQL的子查询
Hibernate HQL的子查询    }

Hibernate HQL的子查询
Hibernate HQL的子查询}

Hibernate HQL的子查询

 

运行结果:

Hibernate: select testteam0_.id as id1_, testteam0_.name as name1_ from testteam testteam0_ where 11<all (select students1_.age from teststu students1_ where testteam0_.id=students1_.team_id)
team2
Hibernate: select testteam0_.id as id1_, testteam0_.name as name1_ from testteam testteam0_ where 15=any (select students1_.age from teststu students1_ where testteam0_.id=students1_.team_id)
team1

 

HQL子查依赖底层数据库的子查询能力,所以,要根据所使用的数据库来决定是否是用你HQL子查询,为了更多的移植性,最好使用连接查询和分组查询代替子查询