本文主要使用一些測試方法來講解HQL的具體應用
采用一對多的關系映射舉例
Dept
[html]
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
Emp
[html]
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<many
<query name=
<![CDATA[
from Emp where eid<?
]]>
</query>
</class>
</hibernate
測試類
[java]
public class TestHQL {
/**
* Hibernate生成關系模型
*/
@Test
public void test
Configuration cfg = new Configuration(nfigure();
SchemaExport export = new SchemaExport(cfg);
export
}
@Test
public void test
//
Session session = HibernateUtils
for(int i=
Dept dept = new Dept();
dept
Set<Emp> emps = new HashSet<Emp>();
for(int j=
Emp emp = new Emp();
emp
emp
emps
}
dept
session
session
}
session
HibernateUtils
}
/**
* 查詢屬性
* 查詢一個屬性
* 如果對於did屬性
*/
@Test
public void test
Session session = HibernateUtils
String hql =
Query query = session
List<String> dnames = query
for (String dname : dnames) {
System
}
HibernateUtils
}
/**
* 查詢多個屬性
*/
@Test
public void test
Session session = HibernateUtils
String hql =
Query query = session
List<Object[]> dnames = query
for (Object[] o : dnames) {
System
}
HibernateUtils
}
/**
* 查詢多個屬性
* 和無參構造器
*/
@Test
public void test
Session session = HibernateUtils
String hql =
Query query = session
List<Dept> depts = query
for(Dept dept:depts){
System
}
HibernateUtils
}
/**
* 簡單對象查詢
* foreach迭代
* 內部采用的是iterator
* 不會執行該對象的方法
*/
@Test
public void test
Session session = HibernateUtils
String hql =
Query query = session
List<Dept> depts = query
for(Dept dept:depts){
System
}
HibernateUtils
}
/**
* 對象查詢
* 參數的順序從
*/
@Test
public void test
Session session = HibernateUtils
String hql =
Query query = session
query
List<Dept> depts = query
for (Dept dept : depts) {
System
}
HibernateUtils
}
/**
* 對象查詢
*/
@Test
public void test
Session session = HibernateUtils
String hql =
List<Dept> depts = session
setString(
for (Dept dept : depts) {
System
}
HibernateUtils
}
/**
* 查詢所有員工信息
* 分頁查詢
* setFirstResult();確定查詢的起點
* setMaxResult();確定查詢的條數
*/
@Test
public void test
Session session = HibernateUtils
String hql =
int currentPage =
int pageSize =
List<Emp> emps = session
setFirstResult((currentPage
setMaxResults(pageSize)
for (Emp emp : emps) {
System
}
HibernateUtils
}
/**
* 查詢所有員工信息
* 查詢最大頁數
* 首先查詢所有記錄數
*/
@Test
public void test
Session session = HibernateUtils
String hql =
List<Long> list = session
int rec = list
int pageSize =
System
System
HibernateUtils
}
/**
* 使用命名查詢
* 將hql語句寫在映射文件中
* 查詢ID號小於
* from Emp where eid<
* <query name=
<![CDATA[
from Emp where eid<?
]]>
</query>
getNamedQuery()
*/
@Test
public void test
Session session = HibernateUtils
Query query = session
query
List<Emp> emps = query
for (Emp emp : emps) {
System
}
HibernateUtils
}
/**
* 在hibernate 中使用sql查詢
* select * from t_emp;
*/
@Test
public void test
Session session = HibernateUtils
String sql =
SQLQuery query = session
List<Object[]> list = query
for (Object[] o : list) {
System
}
HibernateUtils
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28256.html