Hibernate学习笔记----HQL(2)

分页查询

/**
	 * 分页查询
	 */
	public void testPageQuery()
	{
		String hql = "FROM Employee";
		Query query = session.createQuery(hql);
		
		//查询第3页的内容
		int pageNo = 3;
		int pageSize = 5;
		List<Employee> employees = 
		query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
		System.out.println(employees);
		/**
		 * setFirstResult(int firstResult):设定从那一个对象开始检索,参数firstResult
		 * 表示这个对象在查询结果中的索引位置,索引位置的起始值为0,默认情况下,Query从查询结果中的第一个对象开始检索
		 * 
		 * setMaxResult(int maxResult):设定一次最多检索出的对象的数目,在默认情况下Query和Criteria接口检索出查
		 * 询结果中的所有对象。
		 */
	}

命名化查询

//命名查询,查询语句写在hbm.xml里面,便于维护
	public void testNamedQuery()
	{
		Query query = session.getNamedQuery("salaryEmps");
		List<Employee> employees = query.setDouble("minSal", 600)
									   .setDouble("maxSal", 6000).list();
		System.out.println(employees);
	}
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-7-24 23:26:24 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="cn.limbo.hibernate.entities.Employee" table="EMPLOYEES">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="salary" type="double">
            <column name="SALARY" />
        </property>
        <many-to-one name="department" class="cn.limbo.hibernate.entities.Department" column="DEPARTMENT_ID">
        </many-to-one>
    </class>
    <span style="color:#ff0000;"><query name="salaryEmps"><![CDATA[From Employee e WHERE e.salary > :minSal AND e.salary < :maxSal]]></query></span>
</hibernate-mapping>

属性投影检索

/**
	 * 属性投影检索
	 */
	public void testFieldQuery()
	{
		String hql = "SELECT e.email,e.salary,e.department FROM Employee e WHERE e.department = :department";
		Query query = session.createQuery(hql);
		
		Department department = new Department();
		department.setId(13);
		//一条记录查询出来的结果是一个Object数组
		List<Object[]> result = query.setEntity("department", department).list();
		
		for(Object [] objects : result)
		{
			System.out.println(Arrays.asList(objects));
		}
	}
	
	@Test
	/**
	 * 升级版属性投影检索,推荐使用这个方法,但是必须保证实体类一定要有一个匹配的构造器
	 */
	public void testFieldQuery2()
	{
		String hql = "SELECT new Employee(e.email,e.salary,e.department)"
				+ "FROM Employee e "
				+ "WHERE e.department = :department";
		Query query = session.createQuery(hql);
		
		Department department = new Department();
		department.setId(13);
		//返回的是一个Employee的集合
		List<Employee> result = query.setEntity("department", department).list();
		
		for(Employee emp : result)
		{
			System.out.println(emp.getId() + " " + emp.getEmail() + " " + emp.getSalary() + " " + emp.getDepartment());
		}
	}

报表查询(使用Group By,max等函数)

public void testGroupBy()
	{
		String hql = "SELECT min(e.salary) , max(e.salary) "
				+ "FROM Employee e "
				+ "GROUP BY e.department "
				+ "HAVING min(salary) > :minSal";
		Query query = session.createQuery(hql);
		query.setDouble("minSal", 600);
		
		List<Object[]> result = query.list();
		for(Object[] objects : result)
		{
			System.out.println(Arrays.asList(objects));
		}
		
	}

有一个问题

当时我测试的时候出现了stackoverflow的的错误,这是我看到的资料,挺详细的




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值