Hibernate Hql 总结---laoyang

Posted Ipeter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate Hql 总结---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 总结---laoyang的主要内容,如果未能解决你的问题,请参考以下文章

hibernate的hql查询语句总结

Hibernate HQL查询语句总结

hibernate 之 HQL语句总结

hibernate -- HQL语句总结

Hibernate Hql 总结

hibernate -- HQL语句总结