HQL语法入门学习

上传人:jiups****uk12 文档编号:39995987 上传时间:2018-05-22 格式:DOC 页数:13 大小:63.50KB
返回 下载 相关 举报
HQL语法入门学习_第1页
第1页 / 共13页
HQL语法入门学习_第2页
第2页 / 共13页
HQL语法入门学习_第3页
第3页 / 共13页
HQL语法入门学习_第4页
第4页 / 共13页
HQL语法入门学习_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《HQL语法入门学习》由会员分享,可在线阅读,更多相关《HQL语法入门学习(13页珍藏版)》请在金锄头文库上搜索。

1、HQL 查询及语法HQL:Hibernate Query LanguageHQL 是完全面向对象的查询语言,因此可以支持继承和多态等特征。HQL 查询依赖于 Query 类,每个 Query 实例对应一个查询对象,使用 HQL 查询按如下步骤进行:(1)获取 Hibernate Session 对象;(2)编写 HQL 语句;(3)以 HQL 语句作为参数,调用 Session 的 createQuery 方法创建查询对象;(4)如果 HQL 语句包含参数,调用 Query 的 setXxx 方法为参数赋值;(5)调用 Query 对象的 list 等方法遍历查询结果。查询示例:public

2、class HqlQuerypublic static void main(String args) throws Exception HqlQuery mgr = new HqlQuery();/调用查询方法mgr.findPersons();/调用第二个查询方法mgr.findPersonByHappenDate();HibernateUtil.sessionFactory.close();/第一个查询方法private void findPersons() /获得 Hibernate SessionSession sess = HibernateUtil.currentSession()

3、;/开始事务Transaction tx = sess.beginTransaction();/以 HQL 语句创建 Query 对象/执行 setString 方法为 HQL 语句的参数赋值/Query 调用 list 方法访问查询的全部实例List p1 = sess.createQuery(“from Person p where o.myEvents.title = :eventTitle“).setString(“eventTitle“, “很普通事情“).list();/遍历查询的全部结果for (Iterator pit = p1.iterator(); pit.haxNext(

4、); )Person p = (Person)pit.next();System.out.println(p.getName();/提交事务mit();HibernateUtil.closeSession();/第二个查询方法private void findPersonByHappenDate() throws Exception Session sess = HibernateUtil.currentSession();Transaction tx = sess.beginTransaction();/解析出 Date 对象SimpleDateFormat sdf = new Simple

5、DateFormat(“yyyy-MM-dd“);Date start = sdf.parse(“2007-11-27“);System.out.println(“系统开始通过日期查找人“ + start);/通过 Session 的 createQuery 方法创建 Query 对象/设置参数/返回结果集List pl = sess.createQuery(“from Person p where p.myEvents.happenDate between :firstDateand :endDate“).setDate(“firstDate“, start).setDate(“endDat

6、e“, new Date().list();/遍历结果集for (Iterator pit = pl.iterator(); pit.hasNext(); )Person p = (Person)pit.next();System.out.println(p.getName();mit();HibernateUtil.closeSession();$下面介绍 HQL 语句的语法1.from 子句from Person表明从 Person 持久化类中选出全部的实例。推荐:from Person as p2.select 子句select p.name from Person as pselect

7、 p.name.firstName from Person as pselect new list(p.name, p.address) from Person as pselect new ClassTest(p.name, p.address) from Person as p (有前提)select p.name as personName from Person as pselect new map(p.name as personName) from Person as p (与 new map()结合更普遍)3.聚集函数avg,count,max,min,sumselect cou

8、nt(*) from Personselect max(p.age) from Person as pselect p.name | “ | p.address from Person as p4.多态查询from Person as pfrom java.lang.Object ofrom Named as n5.where 子句from Person where name like “tom%“from Person as p where p.name like “tom%“from Cat cat where cat.mate.name like “kit%“select * from

9、cat_table as table1 cat_table as table2 where table1.mate =table2.id and table1.name like “kit%“from Foo foo where foo.bar.baz.customer.address.city like “fuzhou%“from Cat cat, Cat rival where cat.mate = rival.mateselect cat, matefrom Cat cat, Cat matewhere cat.mate = matefrom Cat as cat where cat.i

10、d = 123from Cat as cat where cat.mate.id = 69from Person as personwhere person.id.country = AUand person.id.medicareNumber = 123456from Account as accountwhere account.owner.id.country = AUand account.owner.id.medicareNumber = 123456from Cat cat where cat.class = DomesticCatfrom Account as a where a

11、.person.name.firstName like “dd%“ / 正确from Account as a where a.person.name like “dd%“ / 错误6.表达式from DomesticCat cat where cat.name between A and Bfrom DomesticCat cat where cat.name in (Foo, Bar, Baz)from DomesticCat cat where cat.name not between A and Bfrom DomesticCat cat where cat.name not in (

12、Foo, Bar, Baz)from DomesticCat cat where cat.name is nullfrom Person as p where p.address is not nulltrue 1, false 0from Cat cat where cat.alive = truefrom Cat cat where cat.kittens.size 0from Cat cat where size(cat.kittens) 0from Calendar cal where maxelement(cal.holidays) current datefrom Order or

13、der where maxindex(order.items) 100from Order order where minelement(order.items) 10000/操作集合元素select mother from Cat as mother, Cat as kitwhere kit in elements(foo.kittens)/p 的 name 属性等于集合中某个元素的 name 属性select p from NameList list, Person pwhere p.name = some elements(list.names)/操作集合元素from Cat cat w

14、here exists elements(cat.kittens)from Player p where 3 all elements(p.scores)from Show show where fizard in indices(show.acts)/items 是有序集合属性,items0代表第一个元素from Order order where order.items0.id = 1234/holidays 是 map 集合属性,holidaysnational day是代表其中第一个元素select person from Person person, Calendar calenda

15、rwhere calendar.holidaysnational day = person.birthDayand person.nationality.calendar = calendar/下面同时使用 list 集合和 map 集合属性select item from Item item, Order orderwhere order.itemsorder.deliveredItemIndices0 = item and order.id = 11select item from Item item, Order orderwhere order.itemsmaxindex(order.

16、items) = item and order.id = 11select item from Item item, Order orderwhere order.itemssize(order.items) - 1 = itemselect custfrom Product prod,Store storeinner join store.customers custwhere prod.name = widgetand store.location.name in Melbourne, Sydneyand prod = all elements(cust.currentOrder.lineItems)SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_orderFRO

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号