使用HQL查詢最簡單的例子,就是查詢指定類別對應表格的所有資料,例如:
Session
session = sessionFactory.openSession();
Query query = session.createQuery("from User");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getId() + "\t" +
user.getAge() + "\t" +
user.getName());
}
Query query = session.createQuery("from User");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.getId() + "\t" +
user.getAge() + "\t" +
user.getName());
}
也可以指定類別的全名,例如:
Query
query = session.createQuery("from onlyfun.caterpillar.User");
HQL本身不區分大小寫,不過要注意類別的名稱必須區分大小寫。
在查詢類別對應的表格時,需注意到繼承的問題,Hibernate會自動判定繼承關係,如果查詢的類別是某類別的父類別,則會返回與父類別、子類別對應的 所有表格資料,例如如果查詢java.lang.Object,由於Object在Java中是所有類別的父類別,所以下面這個查詢會返回資料庫中所有表 格的資料:
Query
query = session.createQuery("from java.lang.Object");
如果要針對某個屬性作查詢,則可以如下:
Session
session = sessionFactory.openSession();
Query query = session.createQuery("select user.name from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Query query = session.createQuery("select user.name from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
如果要查詢兩個以上的屬性,則如下,查詢的結果會以陣列的方式傳回:
Session
session = sessionFactory.openSession();
Query query = session.createQuery("select user.age, user.name from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
Object[] obj = (Object[]) iterator.next();
System.out.println(obj[0] + "\t" + obj[1]);
}
Query query = session.createQuery("select user.age, user.name from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
Object[] obj = (Object[]) iterator.next();
System.out.println(obj[0] + "\t" + obj[1]);
}
如果User類別提供有適當的建構方法,則可以在使用HQL時直接指定新建一個物件傳回,例如若User如下設計:
package
onlyfun.caterpillar;
public class User {
private Integer id;
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
....
}
public class User {
private Integer id;
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
....
}
則在使用HQL查詢時可以如下:
Session
session = sessionFactory.openSession();
Query query = session.createQuery("select new User(user.name, user.age) from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
User user= (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
Query query = session.createQuery("select new User(user.name, user.age) from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
User user= (User) iterator.next();
System.out.println(user.getAge() + "\t" + user.getName());
}
要注意的是,這個返回的User實例並未與資料庫有任何關聯,可以試著取得id屬性,可以發現它的值是nul,如果試圖使用Session的 saveOrupdate()方法,則會新增一筆資料而不是更新原有的資料。
可以使用distinct去除資料重複的記錄:
Query
query = session.createQuery("select distinct user.age from User as
user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
您也可以在HQL中使用函式,例如取得資料的筆數:
Query query =
session.createQuery("select count(*) from User as user");
List names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
使用avg()取得屬性的平均值:
Query query =
session.createQuery("select avg(user.age) from User as user");
List
names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
使用upper()函式將字串轉為大寫:
Query query =
session.createQuery("select upper(user.name) from User as user");
List
names = query.list();
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Iterator iterator = names.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
可以一併參考 Query 的使用。