HQL(Hibernate Query Language)本身不区分大小写,不过要注意类别的名称必须区分大小写。
一、查询
Sessionsession =sessionFactory.openSession();
Queryquery = session.createQuery("select user.name from User as user whereuser.age > ? ");
query.setInteger(0,25);
Listnames = query.list();
......
session.close();
您可以使用命名参数(Named Parameter)来取代这个方法,这可以不用依照特定的顺序来设定参数值,并拥有较好的可读性,直接来看个例子:
Sessionsession = sessionFactory.openSession();
Queryquery = session.createQuery("select user.name from User as user whereuser.age > :minAge ");
query.setInteger(" minAge ", 25);
Listnames = query.list();
......
session.close();
也可以将HQL写在程式之外,以避免硬编码(Hardcode)在程式之中,在需要修改HQL时就很方便,在*.hbm.xml中使用<query/>标签,并在< ;![CDATA[与]]>之间撰写HQL,撰写的位置是在</hibernate-mapping>之前,例如:
User.hbm.xml
......
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
<query name="onlyfun.caterpillar.QueryUser">
<![CDATA[
select user.name from User as user where user.age > :minAge
]]>
</query>
</hibernate-mapping>
<query>的name属性用来设定查询外部HQL时的名称依据,使用的例子如下:
Sessionsession = sessionFactory.openSession();
Queryquery = session.getNamedQuery("onlyfun.caterpillar.QueryUser");
query.setInteger(" minAge ", 25);
Listnames = query.list();
……
session.close();
二、查询方法、函数举例
select distinct user.age from User as user
select count(*) from User as user
select avg(user.age) from User as user
from User user where user.name like 'cater%'
from User user order by user.age desc
from User user where user.name is not null
select user.sex, avg(user.age) from Useruser group by user.sex
select user.sex, avg(user.age) from Useruser group by user.sex having avg(user.age) > 20
三、普通SQL查询
Hibernate提供了对SQL的支援,您可以指定您所要建立的SQL,并将实体类别与资料表格关联,举个例子来说,如果您打算使用像以下的SQL语句:
SELECT* FROM user WHERE age > 20
则您可以如下建立SQL查询:
// SQL,并指定别名为user
String sql = "select {user.*} from User user where user.age > 20";
Session session = sessionFactory.openSession();
//建立SQLQuery
SQLQuery sqlQuery = session.createSQLQuery(sql);
//将别名user与实体类User关联在一起
sqlQuery.addEntity("user", User.class);
Iteratoriterator = sqlQuery.list().iterator();
while(iterator.hasNext()){
User user = (User) iterator.next();
}
session.close();
addEntity()是将实体类别与别名连结在一起的方法,大括号指定要查询的资料,Hibernate根据所给定的SQL自动生成以下的句子:
selectuser.id as id0_, user.name as name0_0_, user.age as age0_0_ from Useruser where user.age > 20
返回的结果则由Hibernate进行封装为所指定别名关联之实体类,如此您可以得到使用SQL的弹性,但无需处理繁琐的ResultSet。
您也可以将SQL语句定义在映射文件中,例如:
User.hbm.xml
......
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="user">
....
</class>
<sql-query name="onlyfun.caterpillar.QueryUser">
<![CDATA[
select {user.*} from User user where user.age > :age
]]>
<return alias="user" class="onlyfun.caterpillar.User"/>
</sql-query>
</hibernate-mapping>
使用Hibernate查询时如下:
Sessionsession = sessionFactory.openSession();
Queryquery = session.getNamedQuery("onlyfun.caterpillar.QueryUser");
query.setInteger("age",20);
Iteratoriterator = query.list().iterator();
while(iterator.hasNext()){
User user = (User) iterator.next();
}
session.close();
Hibernate3的映射文件中新增了<sql-insert>、<sql-update>与<sql-delete>三个标签,您可以在这三个标签中使用SQL自定义您的INSERT、UPDATE、DELETE,也就是储存、更新、删除资料时的行为,例如:
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
......
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
<sql-insert>
INSERT INTO user (name, age) VALUES (?, ?)
</sql-insert>
<sql-update>
UPDATE user SET name=?, age=?, WHERE id=?
</sql-update>
<sql-delete>
DELETE FROM user WHERE id=?
</sql-delete>
</class>
</hibernate-mapping>
?对应的顺序是映射文件中属性出现的顺序,假设您储存物件:
session= sessionFactory.openSession();
tx= session.beginTransaction();
session.save(user);
tx.commit();
session.close();
则执行结果中显示的SQL语句会是您自定义的语句,而不是由Hibernate自动生成的语句。
0 Comments:
发表评论