2014-03-04 113 views
0

我有一個要求,我需要應用搜索關鍵字的名稱和和父實體(客戶)的狀態,也是子實體順序的名稱屬性的要求。如何添加休眠「createAlias」子限制到父實體限制

List<Criterion> aliasRestrictionsCriterion = new ArrayList<Criterion>(); 
Junction quotedLikeRestrictions = Restrictions.disjunction(); 
quotedLikeRestrictions.add(Restrictions.ilike("customerName", token, MatchMode.ANYWHERE)); 
quotedLikeRestrictions.add(Restrictions.ilike("customerState", token, MatchMode.ANYWHERE)); 
restrictions.add(quotedLikeRestrictions); 

,並在查詢中包括的子表,我正在做以下

Criteria alias = criteria.createAlias("order", "order") 
.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE)); 

Hibernate是爲了產生下面的查詢,但我需要的是有像比較order.name得到補充到父塊。

CUSTOMER this_ inner join ORDER order_ on this_.ORDER_ID=order_.ORDER_ID where 
(lcase(this_.name) like ? or lcase(this_.state) like ?) and (lcase(order_.NAME) 
like ?)   

我想要的是(lcase(order_.NAME)like?)在父塊內。所以我想在下面的

(LCASE(this_.name)什麼樣的?或LCASE(this_.state)什麼樣的?或LCASE(order_.NAME)怎麼樣?)

我怎樣才能做到這一點?

更新:這是我我打電話的標準

protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) { 
    Session session = currentSession(); 
    Criteria criteria = session.createCriteria(getEntityClass()); 

    // apply restrictions. 
    if (restrictions != null) { 
     for (Criterion criterion : restrictions) { 
      criteria.add(criterion); 
     } 
    } 

    Criteria alias = criteria.createAlias("order", "order").add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE)); 

    List list = criteria.list(); 
    if (list != null) { 
     return list; 
    } 
    return Collections.emptyList(); 
} 

更新2:

protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) { 
    Session session = currentSession(); 
    Criteria criteria = session.createCriteria(getEntityClass()); 

    Junction or = Restrictions.disjunction(); 
    or.add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE)); 
    criteria.createAlias("order", "order").add(or); 


    // apply restrictions. 
    if (restrictions != null) { 
     for (Criterion criterion : restrictions) { 
      criteria.add(criterion); 
     } 
    } 

    List list = criteria.list(); 
    if (list != null) { 
     return list; 
    } 
    return Collections.emptyList(); 
} 

出品 選擇.......從客戶THIS_內連接順序order_上this_.ORDER_ID = order_.uid其中(lcase(order_.NAME)like?)和(lcase(this_.name)like或lcase(this_.state)like?)

orderName就在之前,但仍然沒有在裏面第二或陳述...我很困惑。

回答

1

好,加上限制的標準(這將創建一個AND),您必須將其添加到quotedLikeRestrictions脫節的instea:

quotedLikeRestrictions.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE)); 
+0

那是什麼我第一次嘗試,我得到了這個異常 org.hibernate作爲.QueryException:無法解析屬性:order.name:com.company.dao.entities.Customer。 – hajime

+0

可能是因爲您之前創建別名後創建了別名。如果您發佈完整的Criteria創建代碼,則幫助起來會更容易。 –

+0

當前模式是在服務層中創建限制,並將其傳遞給一個DAOBase抽象類,DAO的抽象類基於DAO的當前實體獲取實體類,然後調用條件,並在限制之後調用createAlias已被應用於標準。我應該這樣做嗎? – hajime