使用Hibernate过滤集合元素的两种方法

 (1)通过代码
       
List list = session.createQuery( " from Programer " ).list();
        
        
for (Iterator iter = list.iterator();iter.hasNext();)
        
... {
           Programer p
=(Programer)iter.next();
           Collection pcs
=session.createFilter(p.getComputers(), "where this.id>'3'").list();
           
           
for(Iterator iter1=pcs.iterator();iter1.hasNext();)...{
               Computer c
=(Computer)iter1.next();
               System.out.println(c.getName());
           }
          
}

 

(2)通过HBM文件

    注意,必须设置lazy="true" 否则hibernate会预先加载computer数据,达不到过滤器再过滤的效果

       < set name = " computers "  cascade = " save-update "  lazy = " true " >
            
< key >
                
< column name = " programer_id "  length = " 50 "   />
            
</ key >
            
< one - to - many  class = " com.Computer "   />
            
< filter name = " idFilter "  condition = " id>:idStr " ></ filter >
        
</ set >
    
</ class >
    
< filter - def name = " idFilter " >
       
< filter - param name = " idStr "  type = " string " />
    
</ filter - def >

 

调用过滤器代码

 

List list = session.createQuery( " from Programer " ).list();
        
        
for (Iterator iter = list.iterator();iter.hasNext();)
        
... {
           Programer p
=(Programer)iter.next();
           Filter filter
=session.enableFilter("idFilter");
           filter.setParameter(
"idStr""3");
           
for(Iterator iter1=p.getComputers().iterator();iter1.hasNext();)...{
               Computer c
=(Computer)iter1.next();
               System.out.println(c.getName());
           }
          
        }


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