首页 > 代码库 > Hibernate Hql 总结(2)---laoyang

Hibernate Hql 总结(2)---laoyang

package com.etc.test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

import com.etc.dao.HibernateSessionFactory;
import com.etc.entity.Dept;
import com.etc.entity.Emp;

public class testd {
	//@Test
	public void findAll()
	{
		Session s = HibernateSessionFactory.getSession();
		Query q = s.createQuery(" from Emp emp where emp.ename like ‘%S%‘");
		
		List<Emp> list = q.list();
		
		for(Emp e:list)
		{
			System.out.println(e);
		}
	}
	
	//@Test
	public void findByEname()
	{
		Session s = HibernateSessionFactory.getSession();
		Query q = s.createQuery(" from Emp where ename like ‘%S%‘");
		q.setMaxResults(3);
		q.setFirstResult(0);
		List<Emp> list = q.list();
		
		for(Emp e:list)
		{
			System.out.println(e);
		}
	}
	
	//@Test
	public void group()
	{
		Session s = HibernateSessionFactory.getSession();
		Query q = s.createQuery("select emp.dept,count(emp.empno) from Emp emp group by emp.dept ");
		Iterator it = q.iterate();
		
		while(it.hasNext())
		{
			Object o[] = (Object[]) it.next();
			Dept d = (Dept)o[0]; 
			
			System.out.println(d.getDname()+"员工个数为:"+o[1]);
		}
	}
	@Test
	public void findBySal()
	{
		Session s = HibernateSessionFactory.getSession();
		Query q = s.createQuery("from Emp emp where emp.sal>2000 and dept.deptno = 20");
		List<Emp> list = q.list();
		System.out.println(list);
	}
	
}

  

Hibernate Hql 总结(2)---laoyang