- 我的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));
Malaguna,這讓我感動萬分。但是,crAux2有編譯錯誤「無法從標準轉換爲標準」 – Jake
它使用別名方法進行編譯,但現在只返回來自crAux2的行 – Jake