Hibernate关联表特殊查询(一方)inner join fetch,left join fet

xiaoxiao2025-12-15  9

inner join fetch/*** 一对多中的一方inner join fetch多方* 这里不能用inner join on* 查询结果是它把一方对多方的那个集合里面的对象都查询出来放到一方的集合中*/public List getallByinnerjoinTShop(){   List list=null;   try {    Query query=this.GetSession().createQuery("from TType as t inner join fetch t.TShops");    list=query.list();   } catch (HibernateException e) {    throw e;   }finally{    this.ColseSession();   }   return list;}测试:List list=typedao.getallByinnerjoinTShop();   Iterator iter=list.iterator();   while(iter.hasNext()){    TType type=(TType) iter.next();    System.out.println(type.toString());    Iterator<TShop> it=type.getTShops().iterator();    while(it.hasNext()){     TShop sh=it.next();     System.out.println(sh.toString());    }   }

left join fetch/*** left join fetch 左外连接*/public List getallLeftjoinfetchTShop(){   List list=null;   try {    Query query=this.GetSession().createQuery("from TType as t left join fetch t.TShops");    list=query.list();   } catch (HibernateException e) {    throw e;   }finally{    this.ColseSession();   }   return list;  } 测试:List list=typedao.getallLeftjoinfetchTShop();   Iterator iter=list.iterator();   while(iter.hasNext()){    TType type=(TType) iter.next();    System.out.println(type.toString());    Iterator<TShop> it=type.getTShops().iterator();    while(it.hasNext()){     TShop sh=it.next();     System.out.println(sh.toString());    }   }

left join/*** left join 左外连接* */public List getallLeftjoinTShop(){   List list=null;   try {    Query query=this.GetSession().createQuery("from TType as t left join t.TShops");    list=query.list();   } catch (HibernateException e) {    throw e;   }finally{    this.ColseSession();   }   return list;  }测试:List list=typedao.getallLeftjoinTShop();   Iterator iter=list.iterator();   while(iter.hasNext()){    Object[] obj=(Object[]) iter.next();    TType type=(TType) obj[0];    TShop shop=(TShop) obj[1];    System.out.println(type.toString());    //访问type对象里面的TShops集合里的元素,有懒加载问题    //Iterator<TShop> it=type.getTShops().iterator();    //while(it.hasNext()){    //TShop sh=it.next();    //System.out.println(sh.toString());    //}    //from TType as t left join t.TShops shop可能为空    if(shop!=null){     System.out.println(shop.toString());    }      }隐式内连接/*** 隐式内连接*/public List getall(){   List list=null;   try {    Query query=this.GetSession().createQuery("from TType as t,TShop as s where s.TType=t");    list=query.list();   } catch (HibernateException e) {    throw e;   }finally{    this.ColseSession();   }   return list;}测试:List list=typedao.getall();   Iterator iter=list.iterator();   while(iter.hasNext()){    Object[] obj=(Object[]) iter.next();    TType type=(TType) obj[0];    TShop shop=(TShop) obj[1];    System.out.println(type);    System.out.println(shop);   }

集合过滤/*** 集合过滤*/public List getallfilter(int price){   List list=null;   try {    Query query=this.GetSession().createQuery("from TType");    list=query.list();    Iterator iter=list.iterator();    while(iter.hasNext()){     TType type=(TType) iter.next();     Query que=this.GetSession().createFilter(type.getTShops(), "where SPrice>=?");     que.setInteger(0, price);     List arr=que.list();     type.setTShops(new HashSet(arr));    }   } catch (HibernateException e) {    throw e;   }finally{    this.ColseSession();   }   return list;}测试:List list=typedao.getallfilter(10);   Iterator iter=list.iterator();   while(iter.hasNext()){    TType type=(TType) iter.next();    System.out.println(type.toString());    Iterator<TShop> it=type.getTShops().iterator();    while(it.hasNext()){     TShop sh=it.next();     System.out.println(sh.toString());    }   }

转载请注明原文地址: https://www.6miu.com/read-5040910.html

最新回复(0)