Hibernate中HQL语句的使用

本文主要使用一些测试方法来讲解HQL的具体应用

采用一对多的关系映射举例,两个类Dept{did,dname,emps},Emp{dept,eid,ename}

Dept.hbm.xml中

<hibernate-mapping package="com.tarena.demo.po">
	<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>
			<one-to-many class="Emp"/>
		</set>
	</class>
</hibernate-mapping>


Emp.hbm.xml中

<hibernate-mapping package="com.tarena.demo.po">
	<class name="Emp" table="t_emp">
		<id name="eid" column="eid">
			<generator class="identity"></generator>
		</id>
		<property name="ename"></property>
		<many-to-one name="dept" column="did" class="Dept"></many-to-one>
		<query name="findEmpByEid">
		<![CDATA[
			from Emp where eid<?
		]]>
		</query>
	</class>
</hibernate-mapping>


测试类:

public class TestHQL {
	
	/**
	 * Hibernate生成关系模型
	 */ 
	@Test
	public void test00DDL(){
		Configuration cfg = new Configuration().configure();
		SchemaExport export = new SchemaExport(cfg);
		export.create(true, true);
	}
	@Test
	public void test01InitData(){
		//10个部门,30个员工	
		Session session = HibernateUtils.getSession();
		for(int i=1;i<=10;i++){
			Dept dept = new Dept();
			dept.setDname("部门"+i);
			Set<Emp> emps = new HashSet<Emp>();
			for(int j=1;j<=8;j++){
				Emp emp = new Emp();
				emp.setEname("员工_"+i+"_"+j);
				emp.setDept(dept);
				emps.add(emp);
			}
			dept.setEmps(emps);
			session.save(dept);
			session.clear();
		}
		session.beginTransaction().commit();
		HibernateUtils.close(session);
	}
	
	/**
	 * 查询属性
	 * 查询一个属性,集合中的数据类型为String(对于dname属性),
	 * 如果对于did属性,则是Integer类型
	 */
	@Test
	public void test02(){
		Session session = HibernateUtils.getSession();
		String hql = "select dname from Dept";
		Query query = session.createQuery(hql);
		List<String> dnames = query.list();
		for (String dname : dnames) {
			System.out.println(dname);
		}
		HibernateUtils.close(session);
	}
	
	/**
	 * 查询多个属性,集合中数据的类型为Object[]
	 */
	@Test
	public void test03(){
		Session session = HibernateUtils.getSession();
		String hql = "select did,dname from Dept";
		Query query = session.createQuery(hql);
		List<Object[]> dnames = query.list();
		for (Object[] o : dnames) {
			System.out.println(o[0]+":"+o[1]);
		}
		HibernateUtils.close(session);
	}
	/**
	 * 查询多个属性,动态构建一个对象,Dept类必须要有new Dept(dname,did)构造器
	 * 和无参构造器
	 */
	@Test
	public void test04(){
		Session session = HibernateUtils.getSession();
		String hql = "select new Dept(did,dname) from Dept";
		Query query = session.createQuery(hql);
		List<Dept> depts = query.list();
		for(Dept dept:depts){
			System.out.println(dept.getDname());
		}
		HibernateUtils.close(session);
	}
	
	/**
	 * 简单对象查询
	 * foreach迭代,如果迭代一个空值,如果从该对象获得方法,不会出现空指针异常
	 * 内部采用的是iterator,当it.hasNext()才进行。而如果是空值,it.hasNext()为false
	 * 不会执行该对象的方法,因此不会出现空指针异常。
	 */
	@Test
	public void test05(){
		Session session = HibernateUtils.getSession();
		String hql = "select d from Dept d";
		Query query = session.createQuery(hql);
		List<Dept> depts = query.list();
		for(Dept dept:depts){
			System.out.println(dept.getDname()+","+dept.getDid());
		}
		HibernateUtils.close(session);
	}
	/**
	 * 对象查询,条件查询
	 * 参数的顺序从0开始
	 */
	@Test
	public void test06(){
		Session session = HibernateUtils.getSession();
		String hql = "select d from Dept d where dname=?";
		Query query = session.createQuery(hql);
		query.setString(0, "部门1");
		List<Dept> depts = query.list();
		for (Dept dept : depts) {
			System.out.println(dept.getDname());
		}
		HibernateUtils.close(session);
	}
	
	/**
	 * 对象查询,条件查询
	 */
	@Test
	public void test07(){
		Session session = HibernateUtils.getSession();
		String hql = "select d from Dept d where dname=? and did=?";
		List<Dept> depts = session.createQuery(hql).
			setString(0, "部门1").setInteger(1, 1).list();
		for (Dept dept : depts) {
			System.out.println(dept.getDname());
		}
		HibernateUtils.close(session);
	}
	
	/**
	 * 查询所有员工信息
	 * 分页查询
	 * setFirstResult();确定查询的起点
	 * setMaxResult();确定查询的条数
	 */
	@Test
	public void test08(){
		Session session = HibernateUtils.getSession();
		String hql = "from Emp";
		int currentPage = 5;
		int pageSize = 5;
		List<Emp> emps = session.createQuery(hql).
			setFirstResult((currentPage-1)*pageSize).
				setMaxResults(pageSize).list();
		for (Emp emp : emps) {
			System.out.println(emp.getEname());
		}
		HibernateUtils.close(session);
	}
	
	
	/**
	 * 查询所有员工信息
	 * 查询最大页数
	 * 首先查询所有记录数
	 */
	@Test
	public void test09(){
		Session session = HibernateUtils.getSession();
		String hql = "select count(*) from Emp";
		List<Long> list = session.createQuery(hql).list();
		int rec = list.get(0).intValue();
		int pageSize = 7;
		System.out.println("最大页数为:");
		System.out.println(rec%pageSize == 0?rec/pageSize:rec/pageSize+1);
		HibernateUtils.close(session);
	}
	
	/**
	 * 使用命名查询
	 * 将hql语句写在映射文件中
	 * 查询ID号小于20的员工信息
	 * from Emp where eid<20
	 *  <query name="findEmpByEid">
		<![CDATA[
			from Emp where eid<?
		]]>
		</query>
		
		getNamedQuery(),获取Query对象
	 */
	@Test
	public void test10(){
		Session session = HibernateUtils.getSession();
		Query query = session.getNamedQuery("findEmpByEid");
		query.setInteger(0, 20);
		List<Emp> emps = query.list();
		for (Emp emp : emps) {
			System.out.println(emp.getEid()+":"+emp.getEname());
		}
		HibernateUtils.close(session);
	}
	
	/**
	 * 在hibernate 中使用sql查询
	 * select * from t_emp;
	 */
	@Test
	public void test11(){
		Session session = HibernateUtils.getSession();
		String sql = "select * from t_emp";
		SQLQuery query = session.createSQLQuery(sql);
		List<Object[]> list = query.list();
		for (Object[] o : list) {
			System.out.println(o[0]+":"+o[1]+":"+o[2]);
		}
		HibernateUtils.close(session);
	}
	
}


 

 

你可能感兴趣的:(Hibernate,HQL,HQL,Hibernate)