本文主要使用一些测试方法来讲解HQL的具体应用
采用一对多的关系映射举例两个类Dept{diddnameemps}Emp{depteidename}
Depthbmxml中
[html]
<hibernatemapping package=comtarenademopo>
<class name=Dept table=t_dept>
<id name=did column=did>
<generator class=identity></generator>
</id>
<property name=dname></property>
<set name=emps cascade=all inverse=true>
<key column=did></key>
<onetomany class=Emp/>
</set>
</class>
</hibernatemapping>
Emphbmxml中
[html]
<hibernatemapping package=comtarenademopo>
<class name=Emp table=t_emp>
<id name=eid column=eid>
<generator class=identity></generator>
</id>
<property name=ename></property>
<manytoone name=dept column=did class=Dept></manytoone>
<query name=findEmpByEid>
<![CDATA[
from Emp where eid<?
]]>
</query>
</class>
</hibernatemapping>
测试类
[java]
public class TestHQL {
/**
* Hibernate生成关系模型
*/
@Test
public void testDDL(){
Configuration cfg = new Configuration(nfigure();
SchemaExport export = new SchemaExport(cfg);
exportcreate(true true);
}
@Test
public void testInitData(){
//个部门个员工
Session session = HibernateUtilsgetSession();
for(int i=;i<=;i++){
Dept dept = new Dept();
deptsetDname(部门+i);
Set<Emp> emps = new HashSet<Emp>();
for(int j=;j<=;j++){
Emp emp = new Emp();
empsetEname(员工_+i+_+j);
empsetDept(dept);
empsadd(emp);
}
deptsetEmps(emps);
sessionsave(dept);
sessionclear();
}
sessionbeginTransaction(mit();
HibernateUtilsclose(session);
}
/**
* 查询属性
* 查询一个属性集合中的数据类型为String(对于dname属性)
* 如果对于did属性则是Integer类型
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select dname from Dept;
Query query = sessioncreateQuery(hql);
List<String> dnames = querylist();
for (String dname : dnames) {
Systemoutprintln(dname);
}
HibernateUtilsclose(session);
}
/**
* 查询多个属性集合中数据的类型为Object[]
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select diddname from Dept;
Query query = sessioncreateQuery(hql);
List<Object[]> dnames = querylist();
for (Object[] o : dnames) {
Systemoutprintln(o[]+:+o[]);
}
HibernateUtilsclose(session);
}
/**
* 查询多个属性动态构建一个对象Dept类必须要有new Dept(dnamedid)构造器
* 和无参构造器
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select new Dept(diddname) from Dept;
Query query = sessioncreateQuery(hql);
List<Dept> depts = querylist();
for(Dept dept:depts){
Systemoutprintln(deptgetDname());
}
HibernateUtilsclose(session);
}
/**
* 简单对象查询
* foreach迭代如果迭代一个空值如果从该对象获得方法不会出现空指针异常
* 内部采用的是iterator当ithasNext()才进行而如果是空值ithasNext()为false
* 不会执行该对象的方法因此不会出现空指针异常
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select d from Dept d;
Query query = sessioncreateQuery(hql);
List<Dept> depts = querylist();
for(Dept dept:depts){
Systemoutprintln(deptgetDname()++deptgetDid());
}
HibernateUtilsclose(session);
}
/**
* 对象查询条件查询
* 参数的顺序从开始
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select d from Dept d where dname=?;
Query query = sessioncreateQuery(hql);
querysetString( 部门);
List<Dept> depts = querylist();
for (Dept dept : depts) {
Systemoutprintln(deptgetDname());
}
HibernateUtilsclose(session);
}
/**
* 对象查询条件查询
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select d from Dept d where dname=? and did=?;
List<Dept> depts = sessioncreateQuery(hql)
setString( 部门)setInteger( )list();
for (Dept dept : depts) {
Systemoutprintln(deptgetDname());
}
HibernateUtilsclose(session);
}
/**
* 查询所有员工信息
* 分页查询
* setFirstResult();确定查询的起点
* setMaxResult();确定查询的条数
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = from Emp;
int currentPage = ;
int pageSize = ;
List<Emp> emps = sessioncreateQuery(hql)
setFirstResult((currentPage)*pageSize)
setMaxResults(pageSize)list();
for (Emp emp : emps) {
Systemoutprintln(empgetEname());
}
HibernateUtilsclose(session);
}
/**
* 查询所有员工信息
* 查询最大页数
* 首先查询所有记录数
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String hql = select count(*) from Emp;
List<Long> list = sessioncreateQuery(hql)list();
int rec = listget()intValue();
int pageSize = ;
Systemoutprintln(最大页数为:);
Systemoutprintln(rec%pageSize == ?rec/pageSize:rec/pageSize+);
HibernateUtilsclose(session);
}
/**
* 使用命名查询
* 将hql语句写在映射文件中
* 查询ID号小于的员工信息
* from Emp where eid<
* <query name=findEmpByEid>
<![CDATA[
from Emp where eid<?
]]>
</query>
getNamedQuery()获取Query对象
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
Query query = sessiongetNamedQuery(findEmpByEid);
querysetInteger( );
List<Emp> emps = querylist();
for (Emp emp : emps) {
Systemoutprintln(empgetEid()+:+empgetEname());
}
HibernateUtilsclose(session);
}
/**
* 在hibernate 中使用sql查询
* select * from t_emp;
*/
@Test
public void test(){
Session session = HibernateUtilsgetSession();
String sql = select * from t_emp;
SQLQuery query = sessioncreateSQLQuery(sql);
List<Object[]> list = querylist();
for (Object[] o : list) {
Systemoutprintln(o[]+:+o[]+:+o[]);
}
HibernateUtilsclose(session);
}
}