2012-10-28 55 views
0

我試圖創建具有一個或限制休眠Restcrition標準或與DetachedCritera

DetachedCriteria subquery = DetachedCriteria.forClass(Component.class); 
    subquery.createCriteria("review").add(Restrictions.eq("owner", user)); 
    subquery.createCriteria("review").createCriteria("observers"). 
            add(Restrictions.eq("id", user.getId())); 

所以bascially我需要把Restriction.or的代碼的第二和第三線的上方分離標準,但是Restriction.or不包括DetachedCritera。所以我需要做這樣的事情

Restrictions.or(
     subquery.createCriteria("review").add(Restrictions.eq("owner", user)), 
     subquery.createCriteria("review").createCriteria("observers"). 
            add(Restrictions.eq("id", user.getId())); 

然而,這不會編譯。那麼我如何實現這一目標呢?有什麼建議麼?

在此先感謝。 歡呼聲

回答

0
DetachedCriteria subquery = DetachedCriteria.forClass(Component.class, "component"); 
subquery.createAlias("component.review", "review"); 
subquery.createAlias("review.observers", "observer", Criteria.LEFT_JOIN); 
subquery.add(Restrictions.or(Restrictions.eq("review.owner", user), 
          Restrictions.eq("observer.id", user.getId()))); 
+0

輝煌!有用 –