在 第一個 Hibernate 中介紹如何使用Hibernate在不使用SQL的情況下,以Java中操作物件的習慣來插入數據至資料庫中,當然儲存數據之後,更重要的是如何將資料讀 出,Hibernate中也可以讓您不寫一句SQL,而以Java中操作物件的習慣來查詢數據。
直接來看個範例:
- HibernateDemo.java
package onlyfun.caterpillar;
import java.util.Iterator;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
public class HibernateDemo {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(User.class);
// 查詢user所有欄位
Iterator users = criteria.list().iterator();
System.out.println("id \t name/age");
while(users.hasNext()) {
User user = (User) users.next();
System.out.println(user.getId() +
" \t " + user.getName() +
"/" + user.getAge());
}
// 查詢user中符合條件的欄位
criteria.add(Expression.eq("name", "caterpillar"));
users = criteria.list().iterator();
System.out.println("id \t name/age");
while(users.hasNext()) {
User user = (User) users.next();
System.out.println(user.getId() +
" \t " + user.getName() +
"/" + user.getAge());
}
session.close();
HibernateUtil.shutdown();
}
}
Criteria對SQL進行封裝,對於不甚瞭解SQL的開發人員來說,使用Criteria也可以輕易的進行各種數據的檢索,您也可以使用 Expression設定查詢條件,並將之加入Criteria中對查詢結果作限制,Expression.eq()表示設定符合條件的查詢,例如 Expression.eq("name", "caterpillar")表示設定查詢條件為"name"欄位中為"caterpillar"的資料。
先來看一下執行結果:
Hibernate:
select
this_.id as id0_0_,
this_.name as name0_0_,
this_.age as age0_0_
from
T_USER this_
id name/age
1 caterpillar/30
2 bush/20
Hibernate:
select
this_.id as id0_0_,
this_.name as name0_0_,
this_.age as age0_0_
from
T_USER this_
where
this_.name=?
id name/age
1 caterpillar/30
select
this_.id as id0_0_,
this_.name as name0_0_,
this_.age as age0_0_
from
T_USER this_
id name/age
1 caterpillar/30
2 bush/20
Hibernate:
select
this_.id as id0_0_,
this_.name as name0_0_,
this_.age as age0_0_
from
T_USER this_
where
this_.name=?
id name/age
1 caterpillar/30
Criteria是物件導向式的查詢方式,讓不瞭解SQL的開發人員也可以輕易進行各項查詢,但Criteria的API目前還不是很完善,而 Hibernate鼓勵的查詢方式,是透過HQL(Hibernate Query Language)來進行,直接來看個實例:
- HibernateDemo.java
package onlyfun.caterpillar;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
public class HibernateDemo {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
// 使用HQL建立查詢
Query query = session.createQuery("from User");
Iterator users = query.list().iterator();
System.out.println("id \t name/age");
while(users.hasNext()) {
User user = (User) users.next();
System.out.println(user.getId() +
" \t " + user.getName() +
"/" + user.getAge());
}
System.out.println();
// 使用HQL建立查詢
query = session.createQuery("from User user where user.name like ?");
// 設定查詢參數
query.setParameter(0, "caterpillar");
users = query.list().iterator();
System.out.println("id \t name/age");
while(users.hasNext()) {
User user = (User) users.next();
System.out.println(user.getId() +
" \t " + user.getName() +
"/" + user.getAge());
}
session.close();
HibernateUtil.shutdown();
}
}
執行結果:
Hibernate:
select
user0_.id as id0_,
user0_.name as name0_,
user0_.age as age0_
from
T_USER user0_
id name/age
1 caterpillar/30
2 bush/20
Hibernate:
select
user0_.id as id0_,
user0_.name as name0_,
user0_.age as age0_
from
T_USER user0_
where
user0_.name like ?
id name/age
1 caterpillar/30
select
user0_.id as id0_,
user0_.name as name0_,
user0_.age as age0_
from
T_USER user0_
id name/age
1 caterpillar/30
2 bush/20
Hibernate:
select
user0_.id as id0_,
user0_.name as name0_,
user0_.age as age0_
from
T_USER user0_
where
user0_.name like ?
id name/age
1 caterpillar/30
透過Query介面,您可以先設定查詢參數,之後透過setXXX()等方法,將指定的參數值填入,而不用每次都撰寫完整的HQL,Query的 setParameter()方法第一個參數是指定 ? 出現的位置,從 0 開始,第二個參數則是設定查詢條件。