分享
 
 
 

hibernate查询语句--HQL

王朝other·作者佚名  2006-07-19
窄屏简体版  字體: |||超大  

1 .from

1.1单表查询

from eg.cat as cat.其中,cat只是一个别名,为了用其他子语句的时候书写简单

1.2多表查询

from eg.Cat,eg.Dog

from eg.Cat as cat,eg.Dog as dog

2 join相关

(inner) join

left (outer) join

right (outer) join

full join

HQL同样对SQL中的这些特性支持

下面插播一个小话题,关于上边的那些特性,我一直都没怎么用,今天既然说到这里,就想

把上边的几个特性的用法说一下,也算对自己的一个补充:

假设有两个表:部门、员工,下面列举一些数据:

员工(Employee):

ID Name DepNo

001 Jplateau 01

002 Jony 01

003 Camel 02

部门(Department):

ID Name

01 研发部

02 营销部

在Hibernate中我们操纵的都是对象,所以我们操纵的是部门类和员工类

1).(inner) join

select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name

as name2 from Employee as employee join Department as department on employee.DepNo=

department.ID (注意到条件语句我用on 没有用where)

那么执行结果是什么呢?

id1 name1 id2 name2

++++++++++++++++++++++++++++++++++++++

001 Jplateau 01 研发部

002 Jony 01 研发部

2).left (outer) join

select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name

as name2 from Employee as employee left join Department as department on employee.DepNo=

department.ID

那么执行结果又该是什么呢?

id1 name1 id2 name2

++++++++++++++++++++++++++++++++++++++

001 Jplateau 01 研发部

002 Jony 01 研发部

003 Camel null null

{就是说此时我要已第一个表的记录多少为准,第二个表中没有相应纪录的时候填充null}

3). right (outer) join

select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name

as name2 from Employee as employee right join Department as department on employee.DepNo=

department.ID

那么执行结果又该是什么呢?

id1 name1 id2 name2

++++++++++++++++++++++++++++++++++++++

001 Jplateau 01 研发部

002 Jony 01 研发部

null null 02 营销部

{就是说此时我要已第二个表的记录多少为准,第一个表中没有相应纪录的时候填充null}

3 大小写敏感

4。select语句

就是要确定你要从查询中返回哪些对象或者哪些对象的属性。写几个例子吧:

select employee form Employee as employee

select employee form Employee as employee where employee.Name like 'J%'

select employee.Name form Employee as employee where employee.Name like 'J%'

select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name

as name2 from Employee as employee right join Department as department on employee.DepNo=

department.ID

select elements(employee.Name) from Employee as employee

(不明白elements到底是做什么用的?望给于说明)

等等

5。数学函数

JDO目前好像还不支持此类特性。

avg(...), sum(...), min(...), max(...)

count(*)

count(...), count(distinct ...), count(all...)

其用法和SQL基本相同

select distinct employee.name from Employee as employee

select count(distinct employee.name),count(employee) from Employee as employee

6。polymorphism (暂时不知道如何解释?)

from com.test.Animal as animal

不光得到所有Animal得实例,而且可以得到所有Animal的子类(如果我们定义了一个子类Cat)

一个比较极端的例子

from java.lang.Object as o

可以得到所有持久类的实例

7。where语句

定义查询语句的条件,举几个例子吧:

from Employee as employee where employee.Name='Jplateau'

from Employee as employee where employee.Name like 'J%'

from Employee as employee where employee.Name like '%u'

在where语句中“=”不光可以比较对象的属性,也可以比较对象,如:

select animal from com.test.Animal as animal where animal.name=dog

8。表达式

在SQL语句中大部分的表达式在HQL中都可以使用:

mathematical operators +, -, *, /

binary comparison operators =, >=, <=, <>, !=, like

logical operations and, or, not

string concatenation ||

SQL scalar functions like upper() and lower()

Parentheses ( ) indicate grouping

in, between, is null

JDBC IN parameters ?

named parameters :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法)

SQL literals 'foo', 69, '1970-01-01 10:00:01.0'

Java public static final constants eg.Color.TABBY

其他不必解释了,在这里我只想对查询中的参数问题说明一下:

大家知道在SQL中进行传递参数进行查询的时候,我们通常用PreparedStatement,在语句中写一大堆的“?”,

在hql中也可以用这种方法,如:

List mates = sess.find(

"select employee.name from Employee as employee " +

"where employee.Name=? ",

name,

Hibernate.STRING

);

(说明:上面利用Session里的find方法,在hibernate的api Session中重载了很多find方法,它可以满足你多种形式的查询)

上边是一个参数的情形,这种情况下紧接着引入参数和定义参数的类型,当为多个参数,调用另一个find方法,它的后两个

参数都是数组的形式。

还有另外一种方法来解决上边的问题,JDO也有这样的方法,不过和hibernate的表现形式上有差别,但他们两个骨子里却是

一样的,如:

Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");

q.setString("name", "Jplateau");

//当有多个参数的时候在此逐一定义

Iterator employees = q.iterate();

9。order 语句

和sql语句没什么差别,如:

select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (或者asc)

10。group by 语句

同样和sql语句没什么差别,如:

select employee.name,employee.DepNo from Employee as employee group by employee.DepNo

select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id

{Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects.}

谁帮我解释一下上边两句,谢过!

11。子查询

hibernate同样支持子查询,写几个例子:

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有