Hibernate Query Language(HQL)
Criteria Query
Native SQL
下面對其分別進行解釋
Hibernate Query Language:
HQL提供了是十分強大的功能
from子句
from子句是最簡單的HQL
值得注意的是除了JAVA類和屬性的名稱外
select子句
有時並不需要取得對象的所有屬性
例
{
TRegister user = new TRegister();
Session session = HibernateUtil
Query query = session
List list = query
for(int i =
{
String name = (String)list
System
}
}如果要查詢兩個以上的屬性桧以數組的方式返回
{
TRegister user = new TRegister();
Session session = HibernateUtil
Query query = session
List list = query
for(int i =
{
Object obj[] = (Object[])list
System
}
}
在使用屬性查詢時
{
Session session = HibernateUtil
Query query = session
List list = query
for(int i =
{
//Object obj[] = (Object[])list
TRegister user = (TRegister)list
System
}
/** *//**要正確運行以上程序
public TRegister(String userName
this
this
}
*/
}
統計函數查詢
可以在HQL中使用函數
count()
min()
max()
sum()
avg()
例如
select count(*) from Student
取得Student平均年齡的HQL語句
select avg(s
可以使用distinct去除重復的數據
select distinct s
where子句
HQL也支持子查詢
Query query = session
where子句允許出現的表達式包括了SQL中可以使用的大多數情況
數學操作
真假比較操作
邏輯操作
字符串連接
SQL標題函數
如果查詢返回多條記錄
all
any
some
in
exists
例如
from Group g where
下列語句返回在所有學生中有一個學生的年齡等於
from Group g where
或者
from Group g where
或者
from Group g where
order by子句
查詢返回列表可以按照任何返回的類或者組件的屬性排序
from Student s order by s
連接查詢
與SQL一樣
inner join
left outer join
rigth outer join
full join
下面我重點介紹下內連接查詢
inner join可以簡寫為join
Student stu = null;
Group group = null;
Query query = session
List list = query
Object obj[] = null;
for(int i =
{
obj = (Object[])list
group = (Group)obj[
stu = (Student)obj[
System
}
Criteria Query方式
當查詢數據時
{
Session session = HibernateUtil
Criteria criteria = session
criteria
List list = criteria
TRegister user = (TRegister)list
System
}
常用的查詢限制方法
上面代碼中 Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Restrictions
Order
Order
MatchMode
MatchMode
MatchMode
MatchMode
下面是幾個查詢限制的例子
查詢學生名字以t開關的所有Student對象
Criteria criertia = session
criteria
List list = criteria
Student stu = (Student)list
或者
Criteria criertia = session
criteria
List list = criteria
Student stu = (Student)list
查詢學生姓名在Bill
String[] names = {
Criteria criertia = session
criteria
List list = criteria
Student stu = (Student)list
查詢學生年齡(age)等於
Criteria criertia = session
criteria
criteria
List list = criteria
Student stu = (Student)list
查詢學生姓名以字母F開頭的所有Student對象
Criteria criertia = session
criteria
criteria
List list = criteria
Student stu = (Student)list
注意調用Order
連接限制
Criteria查詢中使用FetchMode來實現連接限制
from Group g
left join fetch g
where g
可以使用Criteria的API完成同樣的功能
Criteria criertia = session
criteria
criteria
List list = criteria
以上兩種方式編寫的代碼都使用相同的SQL語句來完成它們的功能
select g
left outer join Student s
on g
where g
Native SQL查詢
本地SQL查詢指的是直接使用本地數據庫的SQL語言進行查詢
創建一個基於SQL的Query
Native SQL查詢是通過SQLQuery接口來控制的
String sql =
//{}用來引用數據表的別名
SQLQuery sqlQuery = session
sqlQuery
List list = sqlQuery
for(int i =
{
Student stu = (Student)list
}
//createSQLQuery(String sql)利用傳入的sql參數構造一個SQLQuery實例
命名SQL查詢
與HQL的命名查詢相似
如
<sql
<![CDATA[
select {s
]]>
<return alias=
</sql
</hibernate
List list = query
for(int i =
{
Student stu = (Student)list
}
也可以在命名查詢是設定參數
<![CDATA[
select {s
]]>
<return alias=
</sql
</hibernate
query
List list = query
for(int i =
{
Student stu = (Student)list
}自定義insert
Hibernate
<sql
insert into t_student(name
</sql
<sql
update t_student set name=?
</sql
<sql
delete from t_student where id = ?
</sql
</hibernate
在程序中實現以上自定義的insert語句如下
stu
stu
session
<sql
insert into t_student(age
</sql
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26346.html