Criteria 外键匹配查询

package jd;


import java.util.List;


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.Projections;
import org.hibernate.criterion.Restrictions;


import com.oracle.jingdong.entity.Goods;
import com.oracle.jingdong.entity.ThirdClass;


import entity.Student;


public class quchong {
public static void main(String[] args) {
Configuration cf=new Configuration().configure();
SessionFactory sf=cf.buildSessionFactory();
   Session session=sf.openSession();
   Transaction ts=session.beginTransaction();
   
   //Criteria cr=session.createCriteria(Goods.class);
  /*cr.setProjection( Projections.projectionList().
    add( Projections.groupProperty("thirdClass"))
    .add(Projections.groupProperty("brand")));*/
  // cr.createAlias("brand", "mt");
  
//    cr.setProjection(Projections.projectionList().
//     add( Projections.groupProperty("brand")));
     //cr.setProjection(Projections.distinct(Projections.property("thirdClass")));
  // cr.setProjection(Projections.distinct(Projections.property("brand")));
   //cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 
   //cr.add(Restrictions.eq("brand.brandId", 1) );
  
  /*cr.setProjection( Projections.projectionList().
    add( Projections.groupProperty("thirdClass"))
    .add(Projections.groupProperty("brand")));*/
  // cr.createAlias("brand", "mt");
  
//    cr.setProjection(Projections.projectionList().
//     add( Projections.groupProperty("brand")));
    //cr.setProjection(Projections.distinct(Projections.property("thirdClass")));
  // cr.setProjection(Projections.distinct(Projections.property("brand")));
   //cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 
   Criteria cr=session.createCriteria(Goods.class);//brandId为goods外键,为Brand主键
   //cr.add(Restrictions.eq("brand.brandId", 1));//是外键的直接用,没问题
   //cr.add(Restrictions.eq("brand.brandName", "aaa"));//报错
   cr.createAlias("brand", "b").add(Restrictions.eq("b.brandName", "aaa"));//不是外键的定义别名不报错

   List objList=cr.list();
  // System.out.println("dddddddddddd"+cr.list().size());
       for(Object obj:objList)
        System.out.println(((Goods)obj).getGoodsTitle());
    ts.commit();
}


}
上一篇:Latex中cls和sty文件有何区别?


下一篇:React Native在特赞的应用与实践