??xml version="1.0" encoding="utf-8" standalone="yes"?>国内精品在线播放,一区二区三区四区在线播放,成人一区视频http://www.aygfsteel.com/zilong/category/30722.html <center> 新的征?..... </center>zh-cnThu, 10 Apr 2008 10:05:24 GMTThu, 10 Apr 2008 10:05:24 GMT60hibernate 三种查询方式 http://www.aygfsteel.com/zilong/archive/2008/04/10/191937.html阿伟阿伟Thu, 10 Apr 2008 09:41:00 GMThttp://www.aygfsteel.com/zilong/archive/2008/04/10/191937.htmlhttp://www.aygfsteel.com/zilong/comments/191937.htmlhttp://www.aygfsteel.com/zilong/archive/2008/04/10/191937.html#Feedback0http://www.aygfsteel.com/zilong/comments/commentRss/191937.htmlhttp://www.aygfsteel.com/zilong/services/trackbacks/191937.html转自Qhttp://dev.csdn.net/article/68/68297.shtm
hibernate 三种查询方式     选择?wangyihust ?Blog 
 

Q一QHQL

HQLQ?/span>Hibernate Qusery LanguageQ如果你已经熟悉它,׃发现它跟SQL非常相像。不q?/span>你不要被表面的假象迷惑,HQL是面向对象的Q?/span>OOQ用生命的眼光看待每一个对象,他们是如?/span>鲜活Q。如果你?/span>JAVA?/span>SQL语句有一定了解的话,那么HQL对你直易如反掌,你完全可以利用在公R上的旉掌握它?/span>

以下从几个方面进行慢慢深入:

1
。大些敏感
大家知道SQL-92 Query是对大小写不敏感的,但是?/span>HQLQ前面提到它?/span>OO的)中对对象cȝ名称和属性确实大写敏感的(W合java~程语法Q?/span>

HQL 子句本n大小写无养I但是其中出现的类名和属性名必须注意大小写区?/span>
如:sElect cat.name from Cat as cat?/span>select cat.name from Cat as cat是一L
但是Q?/span>
sElect
cat.name from CAT as cat?/span>select cat.name from Cat as cat实不一L?/span>

2
?/span>from语句
最单的Q?/span>
from eg.Cat
它只是简单的q回所?/span>eg.Cat的实?/span>,通常我们此时会ؓeg.Cat其个别名Q因为在query的其余部分可能会用到(参看上边关于大小写敏感时的例子情?/span>)Q如Q?/span>
from eg.Cat as cat q里as可以省略?/span>


上边只是单表查询Q多表的情况如下写法Q?/span>
from eg.Cat, eg.Dog
from eg.Cat as cat, eg.Dog as dog

3
?/span>join相关
(inner) join
left (outer) join
right (outer) join
full join
HQL
同样?/span>SQL中的q些Ҏ支?/span>
下面插播一个小话题Q关于上边的那些Ҏ,我一直都没怎么用,今天既然说到q里Q就x上边的几个特性的用法说一下,也算对自q一个补充:


假设有两个表Q部门、员工,下面列D一些数据:
员工(Employee)Q?/span>
 ID     Name    DepNo
 001   Jplateau   01
 002    Jony        01
 003   Camel      02

部门(Department)Q?/span>
 ID  Name
 01  
研发?/span>
 02   
营销?/span>

?/span>Hibernate中我们操U늚都是对象Q所以我们操U늚是部门类和员?/span>


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 (注意到条件语句我?/span>on 没有?/span>where)
那么执行l果是什么呢Q?/span>
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01
研发?/span>
002 Jony 01
研发?/span>

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
那么执行l果又该是什么呢Q?/span>
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01
研发?/span>
002 Jony 01
研发?/span>
003 Camel null null
{
是说此时我要已W一个表的记录多ؓ准,W二个表中没有相应纪录的时候填?/span>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
那么执行l果又该是什么呢Q?/span>
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01
研发?/span>
002 Jony 01
研发?/span>
null null 02
营销?/span>
{
是说此时我要已W二个表的记录多ؓ准,W一个表中没有相应纪录的时候填?/span>null}

4
?/span>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
Q不明白elements到底是做什么用的?望给于说明)
{等


5。数学函?/span>
JDO
目前好像q不支持此类Ҏ?/span>
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
?/span>polymorphism (暂时不知道如何解释?)
from com.test.Animal as animal
不光得到所?/span>Animal得实例,而且可以得到所?/span>Animal的子c(如果我们定义了一个子c?/span>CatQ?/span>
一个比较极端的例子
from java.lang.Object as o
可以得到所有持久类的实?/span>

7
?/span>where语句
定义查询语句的条Ӟ丑և个例子吧Q?/span>
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'
?/span>where语句?#8220;=”不光可以比较对象的属性,也可以比较对?/span>Q如Q?/span>
select animal from com.test.Animal as animal where animal.name=dog

8
。表辑ּ

?/span>SQL语句中大部分的表辑ּ?/span>HQL中都可以使用Q?/span>
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
Q这U应该是另一U?/span>"?"的变通解x法)

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

Java public static final constants eg.Color.TABBY

其他不必解释了,在这里我只想Ҏ询中的参数问题说明一下:
大家知道?/span>SQL中进行传递参数进行查询的时候,我们通常?/span>PreparedStatementQ在语句中写一大堆?#8220;Q?#8221;Q?/span>
?/span>hql中也可以用这U方?/span>Q如Q?/span>
List mates = sess.find(
"select employee.name from Employee as employee " +
"where employee.Name=? ",
name,
Hibernate.STRING
);
(
说明Q上面利?/span>Session里的findҎQ在hibernate?/span>api Session中重载了很多findҎQ它可以满你多UŞ式的查询)
上边是一个参数的情ŞQ这U情况下紧接着引入参数和定义参数的cdQ当为多个参敎ͼ调用另一?/span>findҎQ它的后两个
参数都是数组的Ş式?/span>

q有另外一U方法来解决上边的问题,JDO也有q样的方法,不过?/span>hibernate的表现Ş式上有差别,但他们两个骨子里却是
一LQ如Q?/span>
Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
q.setString("name", "Jplateau");
//
当有多个参数的时候在此逐一定义
Iterator employees = q.iterate();

9
?/span>order 语句
?/span>sql语句没什么差别,如:
select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (
或?/span>asc)

10
?/span>group by 语句
同样?/span>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.}
谁帮我解释一下上边两句,谢过Q?/span>

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

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

Q二Q条件查询Criteria  Query

。数学函?/span>
JDO
目前好像q不支持此类Ҏ?/span>
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
?/span>polymorphism (暂时不知道如何解释?)
from com.test.Animal as animal
不光得到所?/span>Animal得实例,而且可以得到所?/span>Animal的子c(如果我们定义了一个子c?/span>CatQ?/span>
一个比较极端的例子
from java.lang.Object as o
可以得到所有持久类的实?/span>

7
?/span>where语句
定义查询语句的条Ӟ丑և个例子吧Q?/span>
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'
?/span>where语句?#8220;=”不光可以比较对象的属性,也可以比较对?/span>Q如Q?/span>
select animal from com.test.Animal as animal where animal.name=dog

8
。表辑ּ

?/span>SQL语句中大部分的表辑ּ?/span>HQL中都可以使用Q?/span>
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
Q这U应该是另一U?/span>"?"的变通解x法)

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

Java public static final constants eg.Color.TABBY

其他不必解释了,在这里我只想Ҏ询中的参数问题说明一下:
大家知道?/span>SQL中进行传递参数进行查询的时候,我们通常?/span>PreparedStatementQ在语句中写一大堆?#8220;Q?#8221;Q?/span>
?/span>hql中也可以用这U方?/span>Q如Q?/span>
List mates = sess.find(
"select employee.name from Employee as employee " +
"where employee.Name=? ",
name,
Hibernate.STRING
);
(
说明Q上面利?/span>Session里的findҎQ在hibernate?/span>api Session中重载了很多findҎQ它可以满你多UŞ式的查询)
上边是一个参数的情ŞQ这U情况下紧接着引入参数和定义参数的cdQ当为多个参敎ͼ调用另一?/span>findҎQ它的后两个
参数都是数组的Ş式?/span>

q有另外一U方法来解决上边的问题,JDO也有q样的方法,不过?/span>hibernate的表现Ş式上有差别,但他们两个骨子里却是
一LQ如Q?/span>
Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
q.setString("name", "Jplateau");
//
当有多个参数的时候在此逐一定义
Iterator employees = q.iterate();

9
?/span>order 语句
?/span>sql语句没什么差别,如:
select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (
或?/span>asc)

10
?/span>group by 语句
同样?/span>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.}
谁帮我解释一下上边两句,谢过Q?/span>

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

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

Q二Q条件查询Criteria  Query

 Criteria criteria = osession.createCriteria(Owner.class);
   criteria.add(Expression.eq("age", new Integer(100)));
   criteria.setFirstResult(2);                   //从返回结果的W二条记录开始的5条记?br />    criteria.setMaxResults(5);
   List lc=criteria.list();
   System.out.println("条g查询");
   System.out.println(lc.size());

Q三Q原生SQL语句查询



阿伟 2008-04-10 17:41 发表评论
]]>
վ֩ģ壺 Ϫ| | | | | Ʊ| | | ƽ| Ӵ| ½| Ӷ| °Ͷ| | Ͻ| | | | | | | ˮ| | ƽ| | | | | ǹ| | ͻȪ| ˮ| | ֬| | ³ľ| Զ| ݰ| | Դ| ϳ|