Hibernate inner join fetch,left join fet
inner join fetch
/**
*一对多中的一方innerjoinfetch多方
*这里不能用innerjoinon
*查询结果是它把一方对多方的那个集合里面的对象都查询出来放到一方的集合中
*/
publicListgetallByinnerjoinTShop(){
Listlist=null;
try{
Queryquery=this.GetSession().createQuery("fromTTypeastinnerjoinfetcht.TShops");
list=query.list();
}catch(HibernateExceptione){
throwe;
}finally{
this.ColseSession();
}
returnlist;
}
测试:
Listlist=typedao.getallByinnerjoinTShop();
Iteratoriter=list.iterator();
while(iter.hasNext()){
TTypetype=(TType)iter.next();
System.out.println(type.toString());
Iterator<TShop>it=type.getTShops().iterator();
while(it.hasNext()){
TShopsh=it.next();
System.out.println(sh.toString());
}
}left join fetch
/**
*leftjoinfetch左外连接
*/
publicListgetallLeftjoinfetchTShop(){
Listlist=null;
try{
Queryquery=this.GetSession().createQuery("fromTTypeastleftjoinfetcht.TShops");
list=query.list();
}catch(HibernateExceptione){
throwe;
}finally{
this.ColseSession();
}
returnlist;
}
测试:
Listlist=typedao.getallLeftjoinfetchTShop();
Iteratoriter=list.iterator();
while(iter.hasNext()){
TTypetype=(TType)iter.next();
System.out.println(type.toString());
Iterator<TShop>it=type.getTShops().iterator();
while(it.hasNext()){
TShopsh=it.next();
System.out.println(sh.toString());
}
}left join
/**
*leftjoin左外连接
*
*/
publicListgetallLeftjoinTShop(){
Listlist=null;
try{
Queryquery=this.GetSession().createQuery("fromTTypeastleftjoint.TShops");
list=query.list();
}catch(HibernateExceptione){
throwe;
}finally{
this.ColseSession();
}
returnlist;
}
测试:
Listlist=typedao.getallLeftjoinTShop();
Iteratoriter=list.iterator();
while(iter.hasNext()){
Object[]obj=(Object[])iter.next();
TTypetype=(TType)obj[0];
TShopshop=(TShop)obj[1];
System.out.println(type.toString());
//访问type对象里面的TShops集合里的元素,有懒加载问题
//Iterator<TShop>it=type.getTShops().iterator();
//while(it.hasNext()){
//TShopsh=it.next();
//System.out.println(sh.toString());
//}
//fromTTypeastleftjoint.TShopsshop可能为空
if(shop!=null){
System.out.println(shop.toString());
}
}
隐式内连接
/**
*隐式内连接
*/
publicListgetall(){
Listlist=null;
try{
Queryquery=this.GetSession().createQuery("fromTTypeast,TShopasswheres.TType=t");
list=query.list();
}catch(HibernateExceptione){
throwe;
}finally{
this.ColseSession();
}
returnlist;
}
测试:
Listlist=typedao.getall();
Iteratoriter=list.iterator();
while(iter.hasNext()){
Object[]obj=(Object[])iter.next();
TTypetype=(TType)obj[0];
TShopshop=(TShop)obj[1];
System.out.println(type);
System.out.println(shop);
}集合过滤
/**
*集合过滤
*/
publicListgetallfilter(intprice){
Listlist=null;
try{
Queryquery=this.GetSession().createQuery("fromTType");
list=query.list();
Iteratoriter=list.iterator();
while(iter.hasNext()){
TTypetype=(TType)iter.next();
Queryque=this.GetSession().createFilter(type.getTShops(),"whereSPrice>=?");
que.setInteger(0,price);
Listarr=que.list();
type.setTShops(newHashSet(arr));
}
}catch(HibernateExceptione){
throwe;
}finally{
this.ColseSession();
}
returnlist;
}
测试:
Listlist=typedao.getallfilter(10);
Iteratoriter=list.iterator();
while(iter.hasNext()){
TTypetype=(TType)iter.next();
System.out.println(type.toString());
Iterator<TShop>it=type.getTShops().iterator();
while(it.hasNext()){
TShopsh=it.next();
System.out.println(sh.toString());
}
}