- 浏览: 98934 次
- 性别:
- 来自: 广州
-
最新评论
-
norain1988:
收藏~正在做这样的转换,但是楼主,我遇到一个问题,我以前的项目 ...
在Flash Builder 4中编译Flex 3项目 -
lctexe:
...
在Flash Builder 4中编译Flex 3项目 -
pc_kai:
...
FLEX地图应用教程之四 -
liaoshaoyao:
在FLex4下地图不能拖动呀, 请问楼主有遇到过这个问题没有呀 ...
FLEX地图应用教程之一 -
whao189:
楼主啊。 你那个点击下载 。。。里面是木马啊。。。,,,,,, ...
FLEX地图应用教程之一
HQL:Hibernate Query Language
HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征。
HQL查询依赖于Query类,每个Query实例对应一个查询对象,使用HQL查询按
如下步骤进行:
(
1
)获取Hibernate Session对象;
(
2
)编写HQL语句;
(
3
)以HQL语句作为参数,调用Session的createQuery方法创建查询对象;
(
4
)如果HQL语句包含参数,调用Query的setXxx方法为参数赋值;
(
5
)调用Query对象的list等方法遍历查询结果。
查询示例:
public
class
HqlQuery

...
{

public
static
void
main(String[] args)
throws
Exception
...
{
HqlQuery mgr
=
new
HqlQuery();
//
调用查询方法
mgr.findPersons();
//
调用第二个查询方法
mgr.findPersonByHappenDate();
HibernateUtil.sessionFactory.close();
}
//
第一个查询方法

private
void
findPersons()
...
{
//
获得Hibernate Session
Session sess
=
HibernateUtil.currentSession();
//
开始事务
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(); )

...
{
Person p
=
(Person)pit.next();
System.out.println(p.getName());
}
//
提交事务
tx.commit();
HibernateUtil.closeSession();
}
//
第二个查询方法

private
void
findPersonByHappenDate()
throws
Exception
...
{
Session sess
=
HibernateUtil.currentSession();
Transaction tx
=
sess.beginTransaction();
//
解析出Date对象
SimpleDateFormat sdf
=
new
SimpleDateFormat(
"
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 :firstDate
and :endDate
"
)
.setDate(
"
firstDate
"
, start)
.setDate(
"
endDate
"
,
new
Date())
.list();
//
遍历结果集
for
(Iterator pit
=
pl.iterator(); pit.hasNext(); )

...
{
Person p
=
(Person)pit.next();
System.out.println(p.getName());
}
tx.commit();
HibernateUtil.closeSession();
}
}

$下面介绍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%
'

11
.命名查询
<!--
定义命名查询
-->
<
query name
=
"
myNamedQuery
"
>
<!--
此处确定命名查询的HQL语句
-->
from Person as p where p.age
>
?
</
query
>

调用命名查询的示例代码如下:

private
void
findByNamedQuery()
throws
Exception
...
{
Session session
=
HibernateUtil.currentSession();
Transaction tx
=
session.beginTransaction();
System.out.println(
"
执行命名查询
"
);
//
调用命名查询
List pl
=
sess.getNamedQuery(
"
myNamedQuery
"
)
//
为参数赋值
.setInteger(
0
,
20
)
//
返回全部结果
.list();
//
遍历结果集
for
(Integer pit
=
pl.iterator(); pit.hasNext(); )

...
{
Person p
=
(Person)pit.next();
System.out.println(p.getName());
}
tx.commit();
HibernateUtil.closeSession();
}


































































































































































































































































发表评论
-
图解SSH框架配置步骤
2010-05-24 17:58 9831 开发环境ØMyEclipse5.5ØJDK 1.6 ... -
12个Flex常用功能代码
2010-04-24 13:46 7171。 复制内容到系统剪贴板 System.setCl ... -
MyEclipse6.0:struts2+spring2+hibernate3.1整合实例
2010-04-24 10:48 877最近在搞一个项目要求用新技术来开发。于是找到strut ... -
Struts+Hibernate开发实践 分页的实现
2010-04-24 10:44 707在进行web应用开发的时候经常要进行分页处理,经常看到一些人在 ... -
J2EE项目代码编写规范
2010-04-24 10:35 960代码编写规范目的: ... -
SQL操作全集
2010-04-24 10:17 576SQL操作全集 下列语句部分是Mssql语句,不可以 ... -
MyEclipse8.0M1+SVN插件+汉化+优化
2010-04-23 15:32 1139下载了几个版本,因为用到SVN,6.0和7.0版本的安装svn ... -
eclipse 自动补全的设置,不用按 alt-/ 了(转)
2010-04-22 10:11 1197增强Eclipse(MyEclipse)输入代码提示功能 ... -
为myeclipse8.0减压
2010-04-22 09:31 621前言:MyEclipse5.5 大小 ... -
java新式for循环
2010-04-14 14:48 745package com.test.For_Each; imp ... -
一些淡忘了的Java日期时间函数
2010-04-12 11:26 708package test; imp ort java.sq ... -
138万条记录的List()与iterate()速度对比
2010-04-12 11:25 845程序代码 Session sessio ... -
浅谈hibernate性能优化的几点建议
2010-04-12 11:25 8011、针对oracle数据库而言,Fetch Size 是设定J ... -
Eclipse开发经典教程:常用快捷键
2010-04-12 11:24 737想成为编程“高手 ... -
Hibernate事务处理机制
2010-04-12 11:24 792Hibernate是对JDBC的轻量级对象封装,Hiberna ... -
Hibernate不同数据库的连接及 SQL方言
2010-04-12 11:23 703<session-factory> <p ... -
自定义FCKeditor的图片上传功能
2010-04-12 11:22 1418fck_image.js 196行 function ... -
在Tomcat6下配置数据连接池
2010-04-12 11:22 773Tomcat6已经不像Tomcat5一样有web admin了 ... -
String、StringBuffer 和StringBuilder
2010-04-12 11:22 685在写json类中,用到了S ... -
通过实例学习jsp调用存储过程的方法
2010-04-12 11:21 793程序代码 public void sendSmsqu ...
相关推荐
2. **编写HQL语句**:明确查询目标及查询条件。 3. **创建查询对象**:使用`Session`对象的`createQuery`方法创建`Query`对象。 4. **设置参数**:如果HQL语句中有参数,则需通过`setQuery`方法为其赋值。 5. **执行...
《Hibernate HQL查询语法详解》 Hibernate,作为Java领域中广泛应用的对象关系映射(ORM)框架,极大地简化了数据库操作。其内置的HQL(Hibernate Query Language)是一种面向对象的查询语言,它允许开发者以类和...
HQL语句的语法结构与SQL相似,但更注重对象和类的概念。以下将详细介绍HQL语句的几个关键部分: 1. **from 子句**:这是HQL查询的起点,用于指定要查询的持久化类。例如,`from Person as p`表示从`Person`类中选择...
#### 三、HQL语法详解及示例 下面将详细介绍HQL中的几种常见语法及其应用场景。 ##### 1. `FROM`子句 用于指定查询的目标对象或类。例如,查询所有Person对象: ```java from Person ``` 该语句返回的是Person表中...
HQL查询实体时,基本语法为`FROM`后面跟实体类名。例如,查询所有用户可以这样写: ```java String hql = "FROM User user"; List<User> list = session.createQuery(hql).list(); ``` HQL也支持`WHERE`子句,用于...
在深入探讨HQL语法之前,我们首先需要理解什么是HQL。HQL,全称为Hibernate Query Language,是Hibernate框架提供的一种面向对象的查询语言。与SQL(Structured Query Language)不同,HQL是专为ORM(Object-...
### HQL语法总结:实体查询详解 #### 一、实体查询概述 HQL(Hibernate Query Language)是一种面向对象的查询语言,它允许开发者以一种类似于SQL但更接近于面向对象的方式来编写查询。本文将详细介绍HQL中的实体...
### Spring框架中HQL查询方法详解 在Spring框架与Hibernate技术结合使用时,HQL(Hibernate Query Language)作为对象查询语言被广泛应用于数据查询操作之中。本文将详细解析Spring框架中常用的HQL查询方法及其应用...
**HQL(Hibernate Query Language)**是Hibernate框架中推荐的标准查询语言,它是针对对象的查询语言,具有类似于SQL的语法,但更加强调面向对象的特性。HQL被设计用来查询和操作持久化对象,提供了比Criteria查询更...
Hibernate HQL关联查询遵循了SQL中连接查询的概念,但在语法上有所区别,使其更符合面向对象编程的习惯。Hibernate支持以下几种连接查询方式: 1. **内连接(Inner Join)**:`INNER JOIN` 或 `JOIN` ```java ...
### HQL语句语法详解 HQL(Hibernate Query Language)是一种面向对象的查询...通过以上介绍,我们可以看到HQL不仅提供了丰富的语法结构,还支持面向对象的查询方式,极大地简化了数据库查询操作,提高了开发效率。
HQL的基本语法** HQL查询语句结构通常包括以下几个部分:`SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`。这些关键字与SQL中的用法类似,但HQL中的对象和属性是基于Java类和它们的成员。 例如,...
本文将深入探讨HQL多表查询的核心概念、语法结构以及实际应用案例。 ### HQL多表查询的基础概念 HQL支持多种查询方式,包括单表查询、多表查询、连接查询等。其中,多表查询是处理复杂数据关系的关键技术,它允许...
HQL(Hibernate Query Language)是一种强大的面向对象的查询语言,它允许开发人员以一种类似于SQL的方式查询数据库,但使用的是更接近Java对象模型的语法。在处理复杂的数据关系时,多表查询是必不可少的功能,尤其...
2. **易于理解**:HQL的语法结构清晰,容易理解和学习。 3. **平台无关**:由于HQL是基于Hibernate框架的查询语言,因此不受特定数据库的影响,可以在不同的数据库平台上无缝迁移。 4. **灵活性**:HQL支持多种查询...
Hibernate 查询语言(HQL)语法参考 HQL(Hibernate Query Language)是 Hibernate 框架中的一种强大的查询语言,它类似于 SQL 语句,但是它是完全面向对象的查询语言,可以理解继承、多态和关联等概念。 大小写...
HQL 的基本语法: * from 子句:在 Hibernate 中实现数据库表中的数据查询。 * select 子句:选择将对象的属性返回到查询结果集中。 * where 子句:允许你将返回的对象的实例列表的范围缩小。 HQL 的应用示例: *...
2. **编写HQL语句**:HQL的语法类似于SQL,但更注重对象。例如,`from Person p where p.myEvents.title = :eventTitle`这样的查询语句会找出所有`myEvents`标题为`eventTitle`的`Person`对象。 3. **创建Query对象*...
本教程聚焦于“完善HibernateUtil类及HQL查询入门”,让我们深入探讨这两个关键概念。 首先,`HibernateUtil` 类是 Hibernate 教程中常见的一种工具类,它的主要作用是提供对 Hibernate 框架的简单封装,以方便进行...