2016-04-14 170 views
0

我試圖生成此查詢休眠標準:子查詢中休眠的標準API

select count(*) from res_mapping where mop_id = ? and role_name = ? and 
mop_id not in(select mop_id from res_mapping_mod where mop_id = ? and role_name = ?); 

這裏是我的方法:

public static boolean roleHasMenu(String roleName, String mopId) { 
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction tx = null; 
     long count = 0; 
     try { 
      tx = session.beginTransaction(); 
      DetachedCriteria subquery = DetachedCriteria.forClass(ResMappingMod.class); 
      subquery.add(Restrictions.eq("roleName", roleName)); 
      subquery.add(Restrictions.eq("mopId", mopId)); 
      subquery.setProjection(Projections.property("mopId")); 
      Criteria cr = session.createCriteria(ResMapping.class); 
      cr.add(Restrictions.eq("roleName", roleName)); 
      cr.add(Restrictions.eq("mopId", mopId)); 
      cr.add(Subqueries.notIn("mopId", subquery)); 
      count = (Long) cr.setProjection(Projections.rowCount()).uniqueResult(); 
      tx.commit(); 
     } catch (Exception asd) { 
      log.debug(asd.getMessage()); 
      if (tx != null) { 
       tx.rollback(); 
      } 
     } finally { 
      session.close(); 
     } 
     return count > 0; 
    } 

我所得到的是:

select count(*) as y0_ from GLS.RES_MAPPING this_ where this_.ROLE_NAME=? 
and this_.MOP_ID=? and ? not in (select this_.MOP_ID as y0_ from 
GLS.RES_MAPPING_MOD this_ where this_.ROLE_NAME=? and this_.MOP_ID=?) 

之前not in我有一個參數,而不是一個字段。可能是什麼問題?

回答

1

試試這個

cr.add(Subqueries.propertyNotIn( 「ID」,明細標準));

謝謝, Amit Kumar