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);
Iterator iterator = sqlQuery.list().iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
session.close();
Session session = sessionFactory.openSession();
// 建立 SQLQuery
SQLQuery sqlQuery = session.createSQLQuery(sql);
// 將別名user與實體類User關聯在一起
sqlQuery.addEntity("user", User.class);
Iterator iterator = sqlQuery.list().iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
session.close();
addEntity()是將實體類別與別名連結在一起的方法,大括號指定要查詢的資料,Hibernate根據所給定的SQL自動生成以下的句子:
select
user.id as id0_, user.name as name0_0_, user.age as age0_0_ from User
user where user.age > 20
返回的結果則由Hibernate進行封裝為所指定別名關聯之實體類,如此您可以得到使用SQL的彈性,但無需處理繁瑣的ResultSet。
您也可以將SQL語句定義在映射文件中,例如:
- User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<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 > 20
]]>
<return alias="user" class="onlyfun.caterpillar.User"/>
</sql-query>
</hibernate-mapping>
定義的時候,使用<return>標籤指定別名與實體類之關聯,配合映射文件中的定義,您可以如下運行Hibernate:
Session
session = sessionFactory.openSession();
Query query = session.getNamedQuery("onlyfun.caterpillar.QueryUser");
Iterator iterator = query.list().iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
session.close();
Query query = session.getNamedQuery("onlyfun.caterpillar.QueryUser");
Iterator iterator = query.list().iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
session.close();
也可以設定查詢參數,例如:
....
<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>
....
<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查詢時如下:
Session session = sessionFactory.openSession();
Query query = session.getNamedQuery("onlyfun.caterpillar.QueryUser");
query.setInteger("age", 20);
Iterator iterator = query.list().iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
session.close();