一方:
package com.mengya.dao;
import java.util.HashSet;import java.util.Iterator;import java.util.List;
import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Transaction;
import com.mengya.entity.TType;import com.mengya.util.MySessionUilt;
public class TypeDAO extends MySessionUilt { private Transaction tran; /** * 一对多中的一方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; } /** * 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; } /** * 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; } /** * 隐式内连接 */ 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; } /** * 集合过滤 */ 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; } }多方:
package com.mengya.dao;
import java.util.List;
import org.hibernate.Criteria;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Transaction;import org.hibernate.criterion.MatchMode;import org.hibernate.criterion.Restrictions;
import com.mengya.entity.TShop;import com.mengya.util.MySessionUilt;
public class ShopDAO extends MySessionUilt { private Transaction tran; /** * 多条件组合查询 */ public List getallByMoreWhere(String name,double price){ List list=null; try { Criteria cri=this.GetSession().createCriteria(TShop.class); if(name!=null && name.length()>1){ cri.add(Restrictions.like("SName", name,MatchMode.ANYWHERE)); } if(price>0){ cri.add(Restrictions.lt("SPrice", price)); } list=cri.list(); } catch (HibernateException e) { throw e; }finally{ this.ColseSession(); } return list; } /** * 多对一中的多方的inner join on(主对象可以查出从对象,即使主对象中关与从对象的lazy="true"也是可以把从对象查询出来) * 以TShop为主,若TShop的TType为空,则查询后没有这条记录。即查询结果都是非空的 * 这种方式List中存放的是Object的数组,Object[]中的元素才是TShop和TType */ public List getallByinnerjoinTtype(){ List list=null; try { Query query=this.GetSession().createQuery("from TShop as s inner join s.TType"); list=query.list(); } catch (HibernateException e) { throw e; }finally{ this.ColseSession(); } return list; } /** * 多对一中的多方的inner join fetch(主对象可以查出从对象,即使主对象中关与从对象的lazy="true"也是可以把从对象查询出来) * 以TShop为主,若TShop的TType为空,则查询后没有这条记录。即查询结果都是非空的 * 这种方式List中存放的是TShop对象,TShop.TType对象是非空的 */ public List getallByinnerjoinTtype2(){ List list=null; try { Query query=this.GetSession().createQuery("from TShop as s inner join fetch s.TType"); list=query.list(); } catch (HibernateException e) { throw e; }finally{ this.ColseSession(); } return list; } }
测试:
package com.mengya.test;
import java.util.Iterator;import java.util.List;
import com.mengya.dao.ShopDAO;import com.mengya.dao.TypeDAO;import com.mengya.entity.TShop;import com.mengya.entity.TType;
public class Test { public static void main(String[] args) { ShopDAO shopdao=new ShopDAO(); TypeDAO typedao=new TypeDAO(); /*List list=shopdao.getallByMoreWhere("", 50); Iterator cri=list.iterator(); while(cri.hasNext()){ TShop shop=(TShop) cri.next(); System.out.println(shop); }*/ /* 多对一中的多方的inner join on(主对象可以查出从对象,即使主对象中关于从对象的lazy="true"也是可以把从对象查询出来) * 以TShop为主,若TShop的TType为空,则查询后没有这条记录。即查询结果都是非空的 */ /*List list=shopdao.getallByinnerjoinTtype(); Iterator iter=list.iterator(); while(iter.hasNext()){ Object[] obj=(Object[]) iter.next(); TShop shop=(TShop) obj[0]; TType type=(TType) obj[1]; System.out.println(shop.getSName()+" "+shop.getSPrice()+" 所属类别"+shop.getTType().getTName()); System.out.println(type.toString()); }*/ /** * 用的from TShop as s inner join fetch s.TType * 这样list中存放的就是TShop对象,但它会把一方也查出来, * 同样如查TShop对象对应的TType为null,这个TShop也不会查询出来,即查询结果都是非空的。 */ /*List list=shopdao.getallByinnerjoinTtype2(); Iterator iter=list.iterator(); while(iter.hasNext()){ //这样就是错的 //Object[] obj=(Object[]) iter.next(); //TShop shop=(TShop) obj[0]; //TType type=(TType) obj[1]; //System.out.println(shop.getSName()+" "+shop.getSPrice()+" 所属类别"+shop.getTType().getTName()); //System.out.println(type.toString()); TShop shop=(TShop) iter.next(); System.out.println(shop.getSName()+" "+shop.getSPrice()+" 所属类别"+shop.getTType().getTName()); }*/ /*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()); } }*/ /*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()); } }*/ /*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()); } }*/ /** * 隐式内联连 */ /*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); }*/ 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()); } } }
}
