码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate Hql 总结(2)---laoyang

时间:2017-01-07 16:16:09      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:name   etc   sharp   while   gets   rate   dna   port   [1]   

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

标签:name   etc   sharp   while   gets   rate   dna   port   [1]   

原文地址:http://www.cnblogs.com/ipetergo/p/6259428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!