Hibernate的Query查询示例

Hibernate的Query查询方式的一个简单示例
    Classes是一个JavaBean的简单类,此类中只含有属性、构造方法、get/set方法

package test;

import java.util.List;

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

import util.HbnUtil;
import entity.Classes;

public class TestQuery {
   @Test
   public void textQuery1() {
      Session session = HbnUtil.getSession();//从工具类中获取Session
      try {
         //定义查询SQL
         String hql = "from Classes c where c.className like ? "+
                      "and c.total>:totalPerson "+
                      "order by c.className asc";
         //创建查询对象Query
         Query query = session.createQuery(hql);
         //为参数赋值
           query.setString(0,"001");
          query.setInteger("totalPerson",40);
          //执行查询,返回对象集合
            List<Classes> allClasses = query.list();

           for(Classes c : allClasses) {
              System.out.println("满足条件的班级:"+c.getClassName());
           }
           
           e.getTransaction().commit();
      } catch (HibernateException e) {
	  e.printStackTrace();
	  s.getTransaction().rollback();
       } finally{
	  HbnUtil.closeSession(s);
       }   
   }

   /**
	 * 查询部分属性,返回结果是对象数组的列表
	 */
	@Test
	public void testQuery2(){
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();
			//定义查询HQL
			String hql = "select c.className,c.total" +
					" from Classes c " +
					" where c.className like ? " +
					" and c.total>:totalPerson" +
					" order by c.className asc";
			//创建查询对象Query,赋参数值,设置分页
			Query query = s.createQuery(hql)
							.setString(0, "%0901%")
							.setInteger("totalPerson", 40)
							.setFirstResult(1)
							.setMaxResults(2);
							
			//执行查询,返回对象数组集合
			List<Object[]> allClasses = query.list();
			for (Object[] c:allClasses){
				System.out.println("满足条件的班级名称:"+c[0]);
				System.out.println("总人数:"+c[1]);
			}
			
			s.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		} finally{
			HbnUtil.closeSession(s);
		}
	}
	
	/**
	 * 查询部分属性,返回结果封装成特定对象,
	 * 以对象列表形式返回
	 */
	@Test
	public void testQuery3(){
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();
			//定义查询HQL
			String hql = "select new Classes(c.className,c.total)" +
					" from Classes c " +
					" where c.className like ? " +
					" and c.total>:totalPerson" +
					" order by c.className asc";
			//创建查询对象Query
			Query query = s.createQuery(hql);
			//为参数赋值
			query.setString(0, "%0901%");
			query.setInteger("totalPerson", 40);
			//执行查询,返回对象集合
			List<Classes> allClasses = query.list();
			for (Classes c:allClasses){
				System.out.println("满足条件的班级:"+c.getClassName());
			}
			
			s.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		} finally{
			HbnUtil.closeSession(s);
		}
	}
	
	/**
	 * 分页查询
	 */
	@Test
	public void testQuery4(){
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();
			//定义查询HQL
			String hql = "select new Classes(c.className,c.total)" +
					" from Classes c ";
			//创建查询对象Query
			Query query = s.createQuery(hql);
			//分页查询,设置从第几条记录开始
			query.setFirstResult(1);
			//设置查询的记录条数
			query.setMaxResults(2);

			//执行查询,返回对象集合
			List<Classes> allClasses = query.list();
			for (Classes c:allClasses){
				System.out.println("满足条件的班级:"+c.getClassName());
			}
			
			s.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		} finally{
			HbnUtil.closeSession(s);
		}
	}
	
	/**
	 * HQL定义在映射文件中
	 */
	@Test
	public void testQuery5(){
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();
			
			//从映射文件中获取定义的HQL
			Query query = s.getNamedQuery("query5");
			//分页查询,设置从第几条记录开始
			query.setFirstResult(1);
			//设置查询的记录条数
			query.setMaxResults(2);

			//执行查询,返回对象集合
			List<Classes> allClasses = query.list();
			for (Classes c:allClasses){
				System.out.println("满足条件的班级:"+c.getClassName());
			}
			
			s.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		} finally{
			HbnUtil.closeSession(s);
		}
	}
}

你可能感兴趣的:(C++,c,Hibernate,C#,JUnit)