2015-11-06 79 views
0
  • 我的Hibernate bean ContentElementTypeProperty引用另一個Hibernate Bean TestUnitType(多對一)。
  • TestUnitType是ContentElementTypeProperty的字段。
  • 在數據庫中,testunittypeid是表 contentelementtypeproperty中的一列。

我正在尋求從contentelementtypeproperty檢索所有行,其中testunittypeid爲空或testunittypeid = [給定長值]加入列上的休眠標準限制

以下所示的方法將返回0的結果。

然而,如果我刪除: 要麼

.add(Restrictions.isNull("testUnitType")); 

cr.createCriteria("testUnitType") 
       .add(Restrictions.eq("id", tutId)); 

我得到的結果(但顯然不是我需要的所有行)。

如何組合這兩個,以便他們創建一個OR標準,假設每個標準都是在不同的createCriteria調用下創建的?

@Transactional(propagation = Propagation.MANDATORY) 
public List<ContentElementTypeProperty> getContentElementTypePropertiesForTut(Long businessId, Long tutId) 
     throws TestStructureException 
{ 
    SS.getLogger().debug("getContentElementTypePropertiesForTut business id:"+businessId +" tutid: "+tutId); 
    try 
    { 

     Session session = this.sessionFactory.getCurrentSession(); 

     Criteria cr = session.createCriteria(ContentElementTypeProperty.class) 
       .add(Restrictions.isNull("testUnitType")); 
     cr.createCriteria("testUnitType") 
       .add(Restrictions.eq("id", tutId)); 

     cr.createCriteria("business").add(Restrictions.eq("id", businessId)); 

     List<ContentElementTypeProperty> result = cr.list(); 
     SS.getLogger().debug("getContentElementTypePropertiesForNullTutOnly result size:"+result.size()); 
     return result; 
    } 
    catch (Exception e) 
    { 
     SS.getLogger().error(e.getMessage(), e); 

     throw new TestStructureException(e); 
    } 
} 

UPDATE 在從Malagunna的建議,我嘗試使用標準(如下圖)。但是,這隻會返回crAux2的行。

 Criteria cr = session.createCriteria(ContentElementTypeProperty.class); 


    cr.createAlias("testUnitType", "tut"); 

    Criterion crAux1 = Restrictions.isNull("testUnitType"); 
    Criterion crAux2 = Restrictions.eq("tut.id", tutId); 

    cr.add(Restrictions.or(crAux1, crAux2)); 

回答

1

我相信你的問題是你沒有使用DISJUNCTION來結合這兩個限制。

試試這個:

.... 
Session session = this.sessionFactory.getCurrentSession(); 

//Here it starts my solution 
Criteria cr = session.createCriteria(ContentElementTypeProperty.class); 

Criterion crAux1 = Restrictions.isNull("testUnitType"); 
Criterion crAux2 = cr.createCriteria("testUnitType") 
    .add(Restrictions.eq("id", tutId)); 

cr.add(Restrictions.or(crAux1, crAux2)); 
//Here it ends my solution 

cr.createCriteria("business").add(Restrictions.eq("id", businessId)); 
.... 

我不能完全肯定crAux2我寧願之前測試它,但如果這行不通,那我就先創建一個別名testUnitType,然後重寫crAux2

Criterion crAux2 = Restrictions.eq("alias.id", tutId); 

編輯

我一直在重新閱讀休眠標準文檔,也許這種方法[R esolved您的問題:

createAlias("mate", "mt", Criteria.LEFT_JOIN, Restrictions.like("mt.name", "good%")); 

該樣本已直接由Hibernate文檔拍攝和它的字面規定:

這將返回所有的貓與配偶的名字開始與「好」的命令他們的伴侶的年齡,以及所有沒有配偶的貓。

所以,這麼說,我覺得你可以改變你的方法是這樣的:

.... 
Session session = this.sessionFactory.getCurrentSession(); 

//Here it starts my solution 
Criteria cr = session.createCriteria(ContentElementTypeProperty.class); 

cr.createCriteria("testUnitType", "tut", Criteria.LEFT_JOIN, Restrictions.eq("tut.id", tutId));  
//Here it ends my solution 

cr.createCriteria("business").add(Restrictions.eq("id", businessId)); 
.... 

更簡單,這意味着相同的是析取的樣品。

希望它有幫助!

+0

Malaguna,這讓我感動萬分。但是,crAux2有編譯錯誤「無法從標準轉換爲標準」 – Jake

+0

它使用別名方法進行編譯,但現在只返回來自crAux2的行 – Jake