HQL是Hibernate Query Language的縮寫,語法很想SQL,但是HQL是一種面向?qū)ο蟮牟樵冋Z言。SQL的操作對象是數(shù)據(jù)列、表等數(shù)據(jù)庫對象,而HQL操作的是類、實(shí)例、屬性。
HQL查詢依賴于Query類,每個(gè)Query實(shí)例對應(yīng)一個(gè)查詢對象,使用HQL查詢按如下步驟進(jìn)行:
1.獲取Hibernate Session對象
2.編寫HQL語句
3.以HQL語句作為參數(shù),調(diào)用Session的createQuery方法創(chuàng)建查詢對象
4.如果HQL語句包含參數(shù),則調(diào)用Query的setXxx方法為參數(shù)賦值
5.調(diào)用Query獨(dú)享的list()或uniqueResult()方法返回查詢結(jié)果列表
簡單的例子:
@SuppressWarnings("deprecation") public class HibernateUtil { private static final SessionFactory sessionFactory; static { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } public static Session getOpenSession() { return sessionFactory.openSession(); } public static Session getCurrentSession() { return sessionFactory.getCurrentSession(); } }
@Entity public class Employee { private Integer id; private String name; private Integer age; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Basic public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String toString() { return "id:" + id + " " + "name:" + name + " " + "age:" + age; } }
@SuppressWarnings("all") public class HQLDemo { @Test public void testHQL() { Session session = HibernateUtil.getOpenSession(); List<Employee> employeeList = session.createQuery("from Employee as e").list(); for(Employee e : employeeList) System.out.println(e); } @Test public void testHQLHasParameter() { Session session = HibernateUtil.getOpenSession(); List<Employee> employeeList = session.createQuery("from Employee as e where e.name = :personName").setString("personName", "xujianguo").list(); for(Employee e : employeeList) System.out.println(e); } }
HQL查詢的from子句:
from是最簡單的HQL語句,也是最基本的HQL語句,from關(guān)鍵字后緊跟持久化類的類名,如:
from Employee
表名從Employee類中選出全部的實(shí)例
不過我們常用的是這樣做:
from employee as e
這個(gè)e就是Employee的別名,也就是實(shí)例名,推薦這么寫。
HQL查詢的select子句:
select子句用于選擇指定的屬性或直接選擇某個(gè)實(shí)體,當(dāng)然select選擇的屬性必須是from后持久化類包含的屬性,如:
select e.name from Employee as e
select可以選擇任意屬性,即不僅可以選擇持久化類的直接屬性,還可以選擇組件屬性包含的屬性,如:
select e.name.firstName from Employee as e
HQL查詢的聚集函數(shù):
聚集函數(shù):
avg:計(jì)算屬性的平均值
count:統(tǒng)計(jì)選擇對象的數(shù)量
max:統(tǒng)計(jì)屬性值的最大值
min:統(tǒng)計(jì)屬性值的最小值
sum:計(jì)算屬性值的總和
如:
select count(*) from Employee as e
@Test public void testHQLFunction() { Session session = HibernateUtil.getOpenSession(); System.out.println(session.createQuery("select count(*) from Employee as e").uniqueResult()); }
多態(tài)查詢:
HQL不僅會查詢出該持久化類的全部實(shí)例,還會查詢出該類的子類的全部實(shí)例,前提是存在繼承映射。
HQL查詢的where子句:
where子句主要用于篩選選中的結(jié)果,縮小選擇的范圍,如:
from employee as e where e.name like "xjg%"
@Test public void testHQLWhere() { Session session = HibernateUtil.getOpenSession(); List<Employee> employeeList = session.createQuery("from Employee as e where e.name like 'zhou%'").list(); for(Employee e : employeeList) System.out.println(e); }
order by子句:
查詢返回結(jié)合可以根據(jù)類或組件屬性的任何屬性進(jìn)行排序,還可以使用asc或desc關(guān)鍵字指定升序或者降序,如:
from Employee as e order by e.name desc
子查詢:
子查詢中就是查詢語句中還有查詢語句,如:
from Employee as e where e.age > (select p.age from Person as p)
@Test public void testHQLChildQuery() { Session session = HibernateUtil.getOpenSession(); List<Employee> employeeList = session.createQuery("from Employee as e where e.age > (select e1.age from Employee as e1 where e1.name = 'xujianguo')").list(); for(Employee e : employeeList) System.out.println(e); }
命名查詢:
HQL查詢還支持將查詢所用的HQL語句放入配置文件中,而不是代碼中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素來定義命名查詢,這個(gè)<query>元素只需指定一個(gè)name屬性,指定該命名查詢的名字 ,如:
<query name="query"> from Employee as e <query />
Session里提供了一個(gè)getNamedQuery(String name)方法,該方法用于創(chuàng)建一個(gè)Query對象,一旦獲得Query對象,剩下的工作就跟前面的一樣了。
@Test public void testHQLNamedQuery() { Session session = HibernateUtil.getOpenSession(); List<Employee> employeeList = session.getNamedQuery("query").list(); for(Employee e : employeeList) System.out.println(e); }