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();
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值