---
title: "HQL查询及语法"
author: "aijun980204"
date: "2010-04-24T10:45:25Z"
tags: ["Hibernate"]
categories: ["编程语言"]
slug: "hql"
draft: false
lastmod: "2010-04-24T10:45:25Z"
id: 651970
url: "https://aijun980204.iteye.com/blog/651970"
---

HQL:Hibernate Query Language  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) HQL是完全面向对象的查询语言，因此可以支持继承和多态等特征。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) HQL查询依赖于Query类，每个Query实例对应一个查询对象，使用HQL查询按  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 如下步骤进行：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ( 1 )获取Hibernate Session对象；  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ( 2 )编写HQL语句；  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ( 3 )以HQL语句作为参数，调用Session的createQuery方法创建查询对象；  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ( 4 )如果HQL语句包含参数，调用Query的setXxx方法为参数赋值；  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ( 5 )调用Query对象的list等方法遍历查询结果。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 查询示例：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) public   class  HqlQuery  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   static   void  main(String\[] args)  throws  Exception  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         HqlQuery mgr  =   new  HqlQuery();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 调用查询方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         mgr.findPersons();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 调用第二个查询方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         mgr.findPersonByHappenDate();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         HibernateUtil.sessionFactory.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      // 第一个查询方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      private   void  findPersons()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 获得Hibernate Session  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Session sess  =  HibernateUtil.currentSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 开始事务  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Transaction tx  =  sess.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 以HQL语句创建Query对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 执行setString方法为HQL语句的参数赋值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // Query调用list方法访问查询的全部实例  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         List p1  =  sess.createQuery( " from Person p where o.myEvents.title = :  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             eventTitle " ).setString( " eventTitle " ,  " 很普通事情 " ).list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 遍历查询的全部结果  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          for  (Iterator pit  =  p1.iterator(); pit.haxNext(); )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             Person p  =  (Person)pit.next();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             System.out.println(p.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 提交事务  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         tx.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         HibernateUtil.closeSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      // 第二个查询方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      private   void  findPersonByHappenDate()  throws  Exception  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Session sess  =  HibernateUtil.currentSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Transaction tx  =  sess.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 解析出Date对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         SimpleDateFormat sdf  =   new  SimpleDateFormat( " yyyy-MM-dd " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Date start  =  sdf.parse( " 2007-11-27 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " 系统开始通过日期查找人 "   +  start);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 通过Session的createQuery方法创建Query对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 设置参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 返回结果集  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         List pl  =  sess.createQuery(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              " from Person p where p.myEvents.happenDate between :firstDate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             and :endDate " )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                         .setDate( " firstDate " , start)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                         .setDate( " endDate " ,  new  Date())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                         .list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          // 遍历结果集  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          for  (Iterator pit  =  pl.iterator(); pit.hasNext(); )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             Person p  =  (Person)pit.next();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             System.out.println(p.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         tx.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         HibernateUtil.closeSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) $下面介绍HQL语句的语法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 1 .from子句  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 表明从Person持久化类中选出全部的实例。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 推荐：from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 2 .select子句  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select p.name from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select p.name.firstName from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select  new  list(p.name, p.address) from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select  new  ClassTest(p.name, p.address) from Person as p (有前提)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select p.name as personName from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select  new  map(p.name as personName) from Person as p (与new map()结合更普遍)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 3 .聚集函数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) avg,count,max,min,sum  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select count( * ) from Person  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select max(p.age) from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select p.name  ||   ""   ||  p.address from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 4 .多态查询  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from java.lang.Object o  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Named as n  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 5 .where子句  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person where name like  " tom% "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p where p.name like  " tom% "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where cat.mate.name like  " kit% "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     select  *  from cat\_table as table1 cat\_table as table2 where table1.mate  =  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     table2.id and table1.name like  " kit% "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Foo foo where foo.bar.baz.customer.address.city like  " fuzhou% "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat, Cat rival where cat.mate  =  rival.mate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select cat, mate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat, Cat mate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where cat.mate  =  mate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat as cat where cat.id  =   123  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat as cat where cat.mate.id  =   69  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as person  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where person.id.country  =   ' AU '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     and person.id.medicareNumber  =   123456  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Account as account  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where account.owner.id.country  =   ' AU '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     and account.owner.id.medicareNumber  =   123456  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where cat. class   =  DomesticCat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Account as a where a.person.name.firstName like  " dd% "   //  正确  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Account as a where a.person.name like  " dd% "   //  错误  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 6 .表达式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from DomesticCat cat where cat.name between  ' A '  and  ' B '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from DomesticCat cat where cat.name in ( ' Foo ' ,  ' Bar ' ,  ' Baz ' )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from DomesticCat cat where cat.name not between  ' A '  and  ' B '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from DomesticCat cat where cat.name not in ( ' Foo ' ,  ' Bar ' ,  ' Baz ' )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from DomesticCat cat where cat.name is  null  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p where p.address is not  null  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt; property name = " hibernate.query.substitutions " &gt; true   1 ,  false   0 &lt;/ property &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where cat.alive  =   true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where cat.kittens.size  &gt;   0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where size(cat.kittens)  &gt;   0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Calendar cal where maxelement(cal.holidays)  &gt;  current date  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Order order where maxindex(order.items)  &gt;   100  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Order order where minelement(order.items)  &gt;   10000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // 操作集合元素  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select mother from Cat as mother, Cat as kit  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where kit in elements(foo.kittens)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // p的name属性等于集合中某个元素的name属性  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select p from NameList list, Person p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where p.name  =  some elements(list.names)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // 操作集合元素  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat where exists elements(cat.kittens)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Player p where  3   &gt;  all elements(p.scores)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Show show where  ' fizard '  in indices(show.acts)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // items是有序集合属性，items\[0]代表第一个元素  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Order order where order.items\[ 0 ].id  =   1234  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // holidays是map集合属性，holidays\[national day]是代表其中第一个元素  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select person from Person person, Calendar calendar  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where calendar.holidays\[ ' national day ' ]  =  person.birthDay  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     and person.nationality.calendar  =  calendar  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // 下面同时使用list集合和map集合属性  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select item from Item item, Order order  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where order.items\[order.deliveredItemIndices\[ 0 ]]  =  item and order.id  =   11  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select item from Item item, Order order  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where order.items\[maxindex(order.items)]  =  item and order.id  =   11  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select item from Item item, Order order  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where order.items\[size(order.items)  -   1 ]  =  item  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select cust  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Product prod,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     Store store  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     inner join store.customers cust  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where prod.name  =   ' widget '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     and store.location.name in \[ ' Melbourne ' ,  ' Sydney ' ]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     and prod  =  all elements(cust.currentOrder.lineItems)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) SELECT cust.name, cust.address, cust.phone, cust.id, cust.current\_order  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) FROM customers cust,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     stores store,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     locations loc,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     store\_customers sc,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     product prod  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) WHERE prod.name  =   ' widget '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     AND store.loc\_id  =  loc.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     AND loc.name IN ( ' Melbourne ' ,  ' Sydney ' )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     AND sc.store\_id  =  store.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     AND sc.cust\_id  =  cust.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     AND prod.id  =  ALL(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         SELECT item.prod\_id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         FROM line\_items item, orders o  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         WHERE item.order\_id  =  o.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)             AND cust.current\_order  =  o.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 7 .order by子句  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) order by p.name, p.age  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) order by p.name asc, p.age desc  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 8 .group by子句  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select cat.color, sum(cat.weight), count(cat)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) group by cat.color  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // select后出现的id处出现在group by之后，而name属性则出现在聚集函数中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select foo.id, avg(name), max(name)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Foo foo join foo.names name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) group by foo.id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select cat.color, sum(cat.weight), count(cat)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) group by cat.color  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) having cat.color in (eg.Color.TABBY, eg.Color.BLACK)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) join cat.kittens kitten  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) group by cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) having avg(kitten.weight)  &gt;   100  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) order by count(kitten) asc, sum(kitten.weight) desc  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 9 .子查询  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat as fatcat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where fatcat.weight  &gt;  (select avg(cat.weight) from DomesticCat cat)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Cat as cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) where not (cat.name, cat.color) in (  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     select cat.name, cat.color from DomesticCat cat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 10 .fetch关键字  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Person as p join p.scores  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Document fetch all properties order by name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) from Document doc fetch all properties where lower(doc.name) like  ' %cat% '  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 11 .命名查询  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;!-- 定义命名查询 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt; query name = " myNamedQuery " &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;!-- 此处确定命名查询的HQL语句 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     from Person as p where p.age  &gt;   ?  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/ query &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) 调用命名查询的示例代码如下：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) private   void  findByNamedQuery()  throws  Exception  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     Session session  =  HibernateUtil.currentSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     Transaction tx  =  session.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     System.out.println( " 执行命名查询 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      // 调用命名查询  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     List pl  =  sess.getNamedQuery( " myNamedQuery " )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                      // 为参数赋值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                     .setInteger( 0 ,  20 )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                      // 返回全部结果  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                     .list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      // 遍历结果集  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      for  (Integer pit  =  pl.iterator(); pit.hasNext(); )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Person p  =  (Person)pit.next();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println(p.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     tx.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     HibernateUtil.closeSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }