`
walsh
  • 浏览: 431586 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

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
true 1, false 0
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语句语法详解hql语句

    ### HQL语句语法详解 HQL(Hibernate Query Language)是一种面向对象的查询语言,它提供了灵活而强大的机制来查询数据库中的数据,并将其映射到Java对象上。本篇文章将根据给定的信息深入探讨HQL的基本语法结构...

    HQL语句大全HQL语句大全

    ### HQL语句详解 #### 一、HQL简介 HQL(Hibernate Query Language)是Hibernate框架中的查询语言,它提供了面向对象的方式来进行数据库查询。HQL语法接近于SQL但又有所不同,它允许开发者以面向对象的方式来表达...

    hql语句大全hql语句大全

    ### HQL语句详解:精通Hibernate查询语言 #### 引言 HQL(Hibernate Query Language)是Hibernate框架中用于执行数据库操作的一种强大的查询语言。它提供了面向对象的语法,允许开发人员以一种接近于编程语言的方式...

    HQL查询及语法

    2. **编写HQL语句**:根据需求构建HQL查询语句,这一步骤需要熟悉HQL语法结构。 3. **创建Query对象**:利用Session对象的`createQuery`方法,传入HQL语句创建Query实例。 4. **设置参数**:如果HQL语句中包含...

    Hibernate hql查询语法总结

    基础查询是最简单的HQL语句,用于获取所有`Student`对象。例如: ```java from Student ``` 或 ```java select s from Student as s ``` 这里的`as`关键字可以省略,表示从`Student`表中选择所有记录。查询结果将...

    常用的HQL语句下载

    以下是一些常用HQL语句的使用与说明: 1. **HQL更新语句**: 更新操作允许您修改数据库中的对象属性。在示例中,第4行的HQL语句`update PhUser set realName=?`用于更新`PhUser`表中所有记录的`realName`字段。第5...

    强烈建议的HQL语法规则详解

    2. **编写HQL语句**:HQL的语法类似于SQL,但更注重对象。例如,`from Person p where p.myEvents.title = :eventTitle`这样的查询语句会找出所有`myEvents`标题为`eventTitle`的`Person`对象。 3. **创建Query对象*...

    HQL语句详解Select/update/deletefromwhere...

    ### HQL语句详解:Select/update/delete from where... 在探讨HQL(Hibernate Query Language)时,我们首先要了解它是一种用于Hibernate框架中的查询语言,其语法结构与标准SQL查询语言非常相似,但又针对对象关系...

    学习HQL语句

    where 子句的语法类似于 SQL,通过 where 子句可以对行进行过滤。 例如,select o from Order o where o.id='1234' 将返回 id 等于 1234 的所有订单。 三、聚合函数 聚合函数将一类记录(对象)当做一个单位,...

    HQL语法大全,并带有详细的例子

    3. **创建查询对象**:将编写的HQL语句传递给Session的`createQuery`方法来创建一个Query实例。 4. **设置参数**:如果HQL语句中含有参数,则需要通过Query对象的`setXxx`方法为这些参数赋值。 5. **执行查询并遍历...

    hql语句 使用大全

    ### HQL语句使用大全 HQL(Hibernate Query Language)是一种面向对象的查询语言,它提供了灵活而强大的数据检索方式,使开发人员能够更高效地与数据库交互。本文将详细介绍HQL的基本用法及高级特性。 #### 1. ...

    HQL语法入门学习HQL语法入门学习

    在深入探讨HQL语法之前,我们首先需要理解什么是HQL。HQL,全称为Hibernate Query Language,是Hibernate框架提供的一种面向对象的查询语言。与SQL(Structured Query Language)不同,HQL是专为ORM(Object-...

    经典hibernate教程-HQL语句

    HQL的全称是?...HQL语句为:select jd.jdid,jd.jd from TblJd jd。怎样获得并显示查询结果? 使用'?'做占位符的参数查询,怎样设置参数的值? 命名参数查询的语法是? 怎样创建Criteria查询对象?

    hql语句的学习,很有用的东西

    本篇文章将深入探讨HQL语句的学习及其在Hibernate中的应用。 首先,HQL语句的核心特性是它的面向对象性。与SQL不同,HQL直接操作对象和实体,这使得代码更易于理解和维护。例如,如果你想从数据库中获取所有User...

    HQL Eclipse Plugins 配置教程

    不喜欢使用myEclipse的朋友可以尝试下 最近在项目中使用Hibernate,由于需要使用HQL,找了很多资料,大都是MyEclipse中自带的HQL工具。...工具很好用,可以识别出HQL文的语法正确,并且解析为标准SQL语句。

    HQL语句(结合实例)

    ### HQL语句详解 #### 一、HQL概述 HQL(Hibernate Query Language),即Hibernate查询语言,是一种面向对象的查询语言。它类似于SQL语言,但与SQL不同的是,HQL主要关注于对象的获取,而非直接进行数据库层面的...

    Hibernate 经常用的一些HQL语句

    标题:“Hibernate 经常用的一些HQL语句” 描述:“可以让我们更方便的学习Hibernate” ### HQL(Hibernate Query Language)在Hibernate中的应用 HQL是Hibernate框架提供的查询语言,它允许开发者以面向对象的...

    Hibernate查询语言(HQL) 语法参考

    Hibernate 查询语言(HQL)语法参考 HQL(Hibernate Query Language)是 Hibernate 框架中的一种强大的查询语言,它类似于 SQL 语句,但是它是完全面向对象的查询语言,可以理解继承、多态和关联等概念。 大小写...

    hql语句查询

    ### HQL语句查询知识点详解 #### 一、HQL简介 HQL(Hibernate Query Language)是Hibernate框架中用于查询数据的一种语言。它类似于SQL,但面向对象特性更明显,可以更加灵活地处理复杂的对象图关系。HQL支持基本的...

Global site tag (gtag.js) - Google Analytics