`

hql查询语句

SQL 
阅读更多

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.统计函数查询:

1: count() 统计记录的条数
2: min() 求最小值
3: max() 求最大值
4: sum() 求和
4: avg() 求平均值

//取得Student的数量
Query query=session.createQuery("select count(*) from Student")

//avg()取得Student平均年龄
Query query=session.createQuery("select avg(s.age) from Student as s")

//upper()方法将字符串转为大写
Query query=session.createQuery("select upper(s.name) from Student as s")

//去除重复行distinct
Query query=session.createQuery("select distinct s.age from Student as s")

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.表达式
=, <>, >, <, >=, <=, between, not between, in, not in, is, like等。

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
)

like语句:

String hql = "from Users u where u.userName=? and u.userPass=? and u.userInfo like ?";
Object param[] = { userName, password, '%'+info+'%' };

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.子查询:


all 表示所有记录
any 便是所有记录中的任意一条
somy 与any用法一样
in 等价于any
exists 表示子查询至少要返回一条数据

all:
from Team t where 22<all(select s.age from Student s)
from Team t where all(select s.age from t.student s)>22

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%'

11.查询链接:
与SQL查询一样,hql也支持连接查询,如内连接,外连接和交叉连接.支持的链接类型是从ANSI SQL中借鉴来的.
1: inner jion (内连接)
2: left outer join (左外连接)
3: right outer join(右外连接)
4: full join(全连接--不常用)
inner jion 可以简写为join.
正常情况下必须要建关联。

分享到:
评论

相关推荐

    Hibernate HQL查询语句总结.docx

    Hibernate HQL 查询语句总结 Hibernate HQL 查询语句是 Hibernate 框架中的一种查询语言,它提供了更加丰富的和灵活的查询特性,具有类似标准 SQL 语句的查询方式,同时也提供了更加面向对象的封装。以下是 ...

    超级详细的hql查询语句教程

    可以通过映射文件预先定义HQL语句,便于复用: ```xml SELECT s FROM Student s WHERE s.sname = :name ``` #### 四、HQL的嵌套子查询 ##### 4.1 嵌套子查询的概念 嵌套子查询是指在主查询中包含另一个查询的...

    HQL语句大全HQL语句大全

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

    HQL查询及语法

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

    HQL语句 HQL语句

    HQL语句 HQL语句 HQL语句 HQL语句 HQL语句

    hql语句语法详解hql语句

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

    HQL多对多的查询语句分析

    标题"**HQL多对多的查询语句分析**"主要关注的是如何在Hibernate中使用HQL处理数据库中的多对多关联关系查询。在数据库设计中,多对多关系是最复杂的关联类型,它意味着一个实体可以与多个其他实体相关联,反之亦然...

    HQL查询语言基础知识

    HQL 中的单表查询使用 FROM 语句,例如:FROM eg.Cat as cat,其中 cat 只是一个别名,为了用其他子语句的时候书写简单。 2.多表查询 HQL 中的多表查询使用 FROM 语句,例如:FROM eg.Cat as cat, eg.Dog as dog。...

    Hibernate-HQL语句多对多写法

    "Hibernate-HQL语句多对多写法" Hibernate 是一个流行的 Java 持久层框架,它提供了强大的对象关系映射(ORM)功能,使得开发者可以方便地与数据库交互。但是,在使用 Hibernate 时,我们经常会遇到多对多关系的...

    Spring中常用的hql查询方法

    该方法支持通过参数化的方式执行查询,即可以向HQL语句中添加单个参数值。这对于简单的条件查询非常有用。 **示例代码**: ```java List&lt;User&gt; userList = this.getHibernateTemplate().find("from bean.User u ...

    hql模糊查询hql模糊查询

    ### 示例2:直接在HQL语句中拼接模糊查询条件 ```java String hql = "from Subject as s where s.subname like '%" + name + "%' and s.subinfo like '%" + info + "%'"; Query query = session.createQuery(hql); ...

    Hibernate之HQL查询

    HQL查询语句结构通常包括以下几个部分:`SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`。这些关键字与SQL中的用法类似,但HQL中的对象和属性是基于Java类和它们的成员。 例如,假设我们有一个`...

    全面解析HQL语句 非常详细直接的HQL语句的功能介绍

    `from`子句是HQL语句的基础,用于指定查询的数据源。例如,`from Person as p`表示查询的目标是从`Person`实体类中选择所有实例。此子句支持简单的全表扫描,也能够进行更复杂的多表联接查询。 ### 2. select子句 ...

    HQL是hibernate自己的一套查询

    在上面的示例中,我们首先通过`HibernateUtil.getSession()`方法获取到一个`Session`实例,然后定义了一个HQL查询语句,该语句用于从`Admin`表中查询`aname`为`name`的所有记录。最后通过`createQuery`方法创建查询...

    hql总条数.txt

    HQL是一种面向对象的查询语言,它允许开发者以面向对象的方式操作数据库,而无需直接与SQL语句打交道。 ### HQL查询数据库总条数 #### 代码解析 提供的代码段展示了如何使用HQL来获取数据库中特定表或查询结果的...

    hql基础语句

    HQL(Hibernate Query Language)是Hibernate框架中用于...以上内容涵盖了HQL的基础用法,包括ID生成策略、基本查询语句、集合参数传递和不同的传参方式。通过这些知识点,你可以更有效地使用Hibernate进行数据库操作。

    hibernate3.2 (十)HQL查询

    Hibernate会自动将HQL语句转化为相应的SQL,这个过程称为查询优化。开发者可以通过开启Hibernate的日志来查看实际执行的SQL语句,以便进行性能分析和优化。 五、HQL的参数绑定 HQL支持参数绑定,避免SQL注入,提高...

Global site tag (gtag.js) - Google Analytics