HQL:Hibernate Query Language

HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征。

$下面介绍HQL语句的语法

1.from子句

from Person

表明从Person持久化类中选出全部的实例。

推荐:from Person as p

2.select子句

select p.name from Person as p

select p.name.firstName from Person as p

select new list(p.name, p.address) from Person as p

select new ClassTest(p.name, p.address) from Person as p (有前提)

select p.name as personName from Person as p

select new map(p.name as personName) from Person as p (与new map()结合更普遍)

3.聚集函数

avg,count,max,min,sum

select count(*) from Person

select max(p.age) from Person as p

select p.name || "" || p.address from Person as p

4.多态查询

from Person as p

from java.lang.Object o

from Named as n

5.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 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.mate

select cat, mate

from Cat cat, Cat mate

where cat.mate = mate

from Cat as cat where cat.id = 123

from Cat as cat where cat.mate.id = 69

from Person as person

where person.id.country = ''AU''

    and person.id.medicareNumber = 123456

from Account as account

where account.owner.id.country = ''AU''

    and account.owner.id.medicareNumber = 123456

from Cat cat where cat.class = DomesticCat

from Account as a where a.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 ''B''

from DomesticCat cat where cat.name in (''Foo'', ''Bar'', ''Baz'')

from DomesticCat cat where cat.name not between ''A'' and ''B''

from DomesticCat cat where cat.name not in (''Foo'', ''Bar'', ''Baz'')

from DomesticCat cat where cat.name is null

from Person as p where p.address is not null

<property name="hibernate.query.substitutions">true 1, false 0</property>

from Cat cat where cat.alive = true

from Cat cat where cat.kittens.size > 0

from Cat cat where size(cat.kittens) > 0

from Calendar cal where maxelement(cal.holidays) > current date

from Order order where maxindex(order.items) > 100

from Order order where minelement(order.items) > 10000
//操作集合元素

select mother from Cat as mother, Cat as kit

where kit in elements(foo.kittens) //p的name属性等于集合中某个元素的name属性

select p from NameList list, Person p

where p.name = some elements(list.names) //操作集合元素

from Cat cat where exists elements(cat.kittens)

from Player p where 3 > all elements(p.scores)

from Show show where ''fizard'' in indices(show.acts)
//items是有序集合属性,items[0]代表第一个元素

from Order order where order.items[0].id = 1234
//holidays是map集合属性,holidays[national day]是代表其中第一个元素

select person from Person person, Calendar calendar

where calendar.holidays[''national day''] = person.birthDay

    and person.nationality.calendar = calendar
//下面同时使用list集合和map集合属性

select item from Item item, Order order

where order.items[order.deliveredItemIndices[0]] = item and order.id = 11

select item from Item item, Order order

where order.items[maxindex(order.items)] = item and order.id = 11

select item from Item item, Order order

where order.items[size(order.items) - 1] = item

select cust

from Product prod,

    Store store

    inner join store.customers cust

where prod.name = ''widget''

    and store.location.name in [''Melbourne'', ''Sydney'']

    and prod = all elements(cust.currentOrder.lineItems)

SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order

FROM customers cust,

    stores store,

    locations loc,

    store_customers sc,

    product prod

WHERE prod.name = ''widget''

    AND store.loc_id = loc.id

    AND loc.name IN (''Melbourne'', ''Sydney'')

    AND sc.store_id = store.id

    AND sc.cust_id = cust.id

    AND prod.id = ALL(

        SELECT item.prod_id

        FROM line_items item, orders o

        WHERE item.order_id = o.id

            AND cust.current_order = o.id

    )

7.order by子句

from Person as p

order by p.name, p.age

from Person as p

order by p.name asc, p.age desc

8.group by子句

select cat.color, sum(cat.weight), count(cat)

from Cat cat

group by cat.color //select后出现的id处出现在group by之后,而name属性则出现在聚集函数中

select foo.id, avg(name), max(name)

from Foo foo join foo.names name

group by foo.id

select cat.color, sum(cat.weight), count(cat)

from Cat cat

group by cat.color

having cat.color in (eg.Color.TABBY, eg.Color.BLACK)

select cat

from Cat cat

join cat.kittens kitten

group by cat

having avg(kitten.weight) > 100

order by count(kitten) asc, sum(kitten.weight) desc

9.子查询

from Cat as fatcat

where fatcat.weight > (select avg(cat.weight) from DomesticCat cat)

from Cat as cat

where not (cat.name, cat.color) in (

    select cat.name, cat.color from DomesticCat cat

)

10.fetch关键字

from Person as p join p.scores

from Document fetch all properties order by name

from Document doc fetch all properties where lower(doc.name) like ''%cat%''

HQL语法的更多相关文章

  1. jpa 联表查询 返回自定义对象 hql语法 原生sql 语法 1.11.9版本

    -----业务场景中经常涉及到联查,jpa的hql语法提供了内连接的查询方式(不支持复杂hql,比如left join ,right join).  上代码了 1.我们要联查房屋和房屋用户中间表,通过 ...

  2. 查询总结、HQL语法、QBC(QueryByCriteria)深入学习

    1.查询总结 在之前的批量查询练习的时候练习基本五种查询方法的使用: 1.OID查询---根据对象ID进行查询 2.对象属性导航查询: obj.getXXX 3.HQL查询:Query对象查询 4.Q ...

  3. Hibernate 框架 -HQL 语法

    HQL ( Hibernate Query Language ) 查询语言是面向对象的查询语言,也是在 Hibernate 中最常见的.其语法和 SQL 语法有一些相似,功能十分强大,几乎支持除特殊 ...

  4. [转]hql 语法与详细解释

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  5. 一脸懵逼学习Hive的使用以及常用语法(Hive语法即Hql语法)

    Hive官网(HQL)语法手册(英文版):https://cwiki.apache.org/confluence/display/Hive/LanguageManual Hive的数据存储 1.Hiv ...

  6. hql 语法详解

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  7. hql 语法与详细解释<转>

    HQL查询 HQL查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性 ...

  8. 3 hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

  9. hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

随机推荐

  1. vue2.0 网页标题更新实现思路

    一.注册全局指令 1.注册一个全局指令 Vue.directive('title', { inserted: function (el, binding) { document.title = el. ...

  2. [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

    接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...

  3. 关于VisualStudio一运行带中文程序就出错或输出乱码问题的解决

    昨晚纠结了老半天,各种查资料最后终于解决了此问题.今天上午便来编写这篇随笔了!(由于问题已解决,未附上出状况的截图)以下是解决办法: 此问题的原因应是文件的编码问题,选定好出错的文件后,在菜单栏中选择 ...

  4. JAVA提高三:反射总结

    为前期学习过反射,再这里再次复习总结下:[转载请说明来源:http://www.cnblogs.com/pony1223/p/7659210.html ] 一.透彻分析反射的基础_Class类 Cla ...

  5. EasyUI ComboTree无限层级异步加载示例

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EasuUIDemoTree.a ...

  6. JQuery上传插件Uploadify详解及其中文按钮解决方案 .

    Uploadify有一个参数是 buttonText 这个无论你怎么改都不支持中文,因为插件在js里用了一个转码方法把这个参数的值转过码了,解码的地方在那个swf文件里,看不到代码,所以这条路不行. ...

  7. 在不用Promise的情况下如何控制异步请求?

    如何更好的控制异步请求?相信大家一定首选Promise对象.确实,使用Promise控制异步请求确实非常方便,直接使用then()方法就可以实现当一个异步请求完成后再处理另一个请求或操作.同时,这样的 ...

  8. 【学习】js学习笔记:数组(二)

    二维数组 例子:矩形反转: <script> var arr=[[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]]; ...

  9. 使用Olami SDK 语音控制一个支持HomeKit的智能家居的iOS程序

    前言 HomeKit是苹果发布的智能家居平台.通过HomeKit组件,用户可以通过iphone.iPad和ipod Touch来控制智能灯泡,风扇.空调等支持HomeKit的智能家居,尤其是可以通过S ...

  10. Servlet中路径信息总结

    ./ 当前目录 ../ 父级目录 / 根目录 资源寻找都是依靠路径,资源存储方式是按照哈希表运算的,所以路径的计算其实就是哈希值的计算. servlet中,所有路径的配置都要用绝对路径. 什么是绝对路 ...