天天看點

Hibernate對象關系的QBC的幾種查詢方法Hibernate對象關系的QBC的幾種查詢方法:

Hibernate對象關系的QBC的幾種查詢方法

  • Hibernate對象關系的QBC的幾種查詢方法:
    • 基本查詢,分組查詢,使用内置聚集函數,組合查詢,分頁查詢

Hibernate對象關系的QBC的幾種查詢方法:

基本查詢,分組查詢,使用内置聚集函數,組合查詢,分頁查詢

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

import javax.persistence.Query;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import javassist.expr.NewArray;

public class test {

	public static void main(String[] args) {
//		Type type = new Type();
//		ProductInfo productInfo = new ProductInfo();
//		productInfo.setId(111);
//		productInfo.setCode("111");
//		productInfo.setName("111");
//		type.setId(111);
//		type.setName("222");
//		
//		type.getPis().add(productInfo);
//		productInfo.setType(type);
		
		
		
		// 生成Configuration執行個體并進行初始化
        Configuration cfg = new Configuration().configure();

        // 從Configuration執行個體中擷取SessionFactory
        SessionFactory sf = cfg.buildSessionFactory();

        // 打開一個Session,準備資料庫讀寫操作
        Session session = sf.openSession();
        Transaction tx = null;
        try {
            // 定義事務
            tx = session.beginTransaction();
            // 儲存班級資訊,并且級聯儲存學生資訊
            //session.save(type);
//            簡單查詢
//            List result = session.createQuery("from ProductInfo").list();
//            for (Iterator iter = result.iterator(); iter.hasNext();){
//                ProductInfo p = (ProductInfo) iter.next();
//                System.out.println(p.getId()+" "+p.getName()+" "+p.getCode()+" "+p.getType());
//            }
            
            //投影查詢(屬性查詢)
//            List result = session.createQuery("select code,name from ProductInfo order by name asc").list();
//            for (Iterator iter = result.iterator(); iter.hasNext();) {
//            	Object[] obj = (Object[])iter.next();
//                System.out.println(obj[0]+" "+obj[1]);
//			}
            
            //聚集查詢
//            List list= session.createQuery("select count(P.price),min(P.price),max(P.price),sum(P.price),avg(P.price) from ProductInfo P").list();
//            for (Iterator iter = list.iterator(); iter.hasNext();) {
//            	Object[] result = (Object[]) iter.next();
//            		System.out.println(result[0]+" "+result[1]+" "+result[2]+" "+result[3]+" "+result[4]);
//            }
            
            //分組查詢
//            List list= session.createQuery("select count(P.type) from ProductInfo P group by P.type").list();
//            for (Iterator iter = list.iterator(); iter.hasNext();) {
//            	Long result = (Long) iter.next();
//            		System.out.println(result);
//            }
            
            //動态執行個體查詢
//            List list= session.createQuery("select new ProductInfo(p.code,p.name) from ProductInfo p order by p.name asc").list();
//            for (Iterator iter = list.iterator(); iter.hasNext();) {
//            	ProductInfo result = (ProductInfo) iter.next();
//            		System.out.println(result.getCode()+"  "+result.getName());
//            }
            //分頁查詢
//            Query query= session.createQuery("from ProductInfo");
//            query.setFirstResult(0);
//            query.setMaxResults(3);
//            List list = ((org.hibernate.query.Query) query).list();
//            for (Iterator iter = list.iterator(); iter.hasNext();) {
//            	ProductInfo result = (ProductInfo) iter.next();
//            	System.out.println(result.getId()+" "+result.getName()+" "+result.getCode());
//            }
            //條件查詢
//            Iterator iter = session.createQuery("select p.id,p.name,p.code from ProductInfo p where p.type=:ID").setInteger("ID", 1).iterate();
//            while(iter.hasNext()) {
//            	Object[] result = (Object[]) iter.next();
//            	System.out.println(result[0]+" "+result[1]+" "+result[2]+" ");
//            }
            //QBC簡單查詢
//            Criteria crit = session.createCriteria(ProductInfo.class);
//            Criterion cond = Restrictions.eq("id",new Integer(1));
//            crit.add(cond);
//            List results = crit.list();
//            for (Iterator iter = results.iterator();iter.hasNext();) {
//				ProductInfo pr = (ProductInfo)iter.next();
//				System.out.println(pr.getId()+" "+pr.getName()+" ");
//			}
            //QBC分組查詢
//            Criteria crit = session.createCriteria(ProductInfo.class);
//            crit.setProjection(Projections.groupProperty("type"));
//            List list = crit.list();
//            Iterator it = list.iterator();
//            while(it.hasNext()){
//            	Type pr = (Type)it.next();
//            	System.out.println(pr.getId()+" "+pr.getName()+" ");
//            }
            //QBC使用内置函數集查詢
//            Criteria crit = session.createCriteria(ProductInfo.class);
//            crit.setProjection(Projections.avg("price"));
//            List list = crit.list();
//            Iterator it = list.iterator();
//            while(it.hasNext()){
//            	System.out.println(it.next().toString());
//            }
            //QBC組合查詢
//	          Criteria crit = session.createCriteria(ProductInfo.class);
//	          Criterion dis = Restrictions.disjunction()
//	        		  .add(Restrictions.like("name","巴%"))
//	        		  .add(Restrictions.like("name", "全%"));
//	          crit.add(Restrictions.conjunction()
//	        		  .add(Restrictions.ge("id",1))
//	        		  .add(dis));
//	          ProjectionList projectionList = Projections.projectionList();
//	          projectionList.add(Projections.property("id"));
//	          projectionList.add(Projections.property("name"));
//	          crit.setProjection(projectionList);
//	          List list = crit.list();
//	          Iterator it = list.iterator();
//	          while(it.hasNext()){
//	        	  Object[] re = (Object[])it.next();
//	          	System.out.println(re[0]+" "+re[1]);
//	          }
            //QBC關聯查詢
//            Criteria crit = session.createCriteria(ProductInfo.class);
            //QBC分頁查詢
//          Criteria crit = session.createCriteria(ProductInfo.class);
//          Criterion cond = Restrictions.eq("id",new Integer(1));
//          crit.add(cond);
//          System.out.println("請分别輸入第一條資料的位置和一頁顯示的資料個數:");
//          Scanner sc = new Scanner(System.in);
//          Scanner sc1 = new Scanner(System.in);
//          int firstD = sc.nextInt();
//          int maxR = sc1.nextInt();
//          crit.setFirstResult(firstD);
//          crit.setMaxResults(maxR);
//          List results = crit.list();
//          for (Iterator iter = results.iterator();iter.hasNext();) {
//				ProductInfo pr = (ProductInfo)iter.next();
//				System.out.println(pr.getId()+" "+pr.getName()+" ");
//			}
//          System.out.println("請輸入你要跳轉的頁面:");
//          Scanner sc2 = new Scanner(System.in);
//          int pageNum = sc2.nextInt();
//          int firstData = firstD + pageNum * maxR;
//          crit.setFirstResult(firstData);
//          crit.setMaxResults(maxR);
//          for (Iterator iter = results.iterator();iter.hasNext();) {
//				ProductInfo pr = (ProductInfo)iter.next();
//				System.out.println(pr.getId()+" "+pr.getName()+" ");
//			}
            // 事務送出
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                // 復原事務
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            // 關閉Session
            session.close();
        }

	}
}
           

繼續閱讀