有一个类:
Class A{
long objectPK;
string objectname;
long parentID;
long priorID;
int type//取值:type=2,type=3,type=6;
string userID;
}
现在有一个hql语句,我想用criteria表示:
hql="from A as obj1 where obj1.userID=sender and obj1.type=2  and obj1.parentID=0 and obj1.objectPK in (select obj2.priorID from A as  obj2 where obj2.userID is null and obj2.type=6 )"
请问这个用hibernate Criteria如何表示?多谢!

解决方案 »

  1.   

    DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
    criteria.add(Restrictions.eq("userID", "sender"))
    .add(Restrictions.eq("userID", "sender"))
    .add(Restrictions.eq("parentID", new Long(0)))
    //下边这段用sql,因为你的class A没有关联,criteria 没办法,当然你也可以先把那些objectPK 查询出来,然后传一个数组进来
    //做法是这样的 .add(Restrictions.in("objectPK ", objPks));//objpks是一个数组
    .add(Restrictions.sqlRestriction(" objectPK in (select obj2.priorID from A as  obj2 where obj2.userID is null and obj2.type=6 ");
      

  2.   

    多写了一个.add(Restrictions.eq("userID", "sender")),去掉