2015-11-11 30 views
0

以下是上面的代碼,我只想將國家級添加到條件中,以便如果countryclass中的deleteflag爲false,則不應該獲取staveprovince類中的狀態:如何在條件中添加多個類來檢查列

public List<StateProvince> getAllState(Country country)throws HibernateException,ConstraintViolationException { 
    Session session = SessionFactoryUtil.getSessionFactory().openSession(); 
    try { 
     session.beginTransaction(); 
     Criteria criteria = session.createCriteria(StateProvince.class); 
     criteria.setCacheable(true); 
     criteria.add(Restrictions.eq("country", country)); 
     criteria.add(Restrictions.eq("deleteFlag", false)); 
     return criteria.list(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     session.getTransaction().commit(); 
     session.close(); 
    } 
} 
+0

你好,請讓我知道的映射。 StateProvince中是否包含Country? – SyntaX

回答

0

我猜countryCountry類型的StateProvince屬性,所以在這種情況下,你的標準必須執行以下操作:

criteria.createCriteria("country").add(Restrictions.eq("deleteFlag", false)); 
+0

我得到了我想要的答案 –

+0

它不會檢查stateprovince deleteflag –

0

我假設你有StateProvince相關國家。請評論,如果這不能解決你的問題。

public List<StateProvince> getAllState(Country country)throws HibernateException,ConstraintViolationException { 
    Session session = SessionFactoryUtil.getSessionFactory().openSession(); 
    try { 
     session.beginTransaction(); 
     Criteria criteria = session.createCriteria(StateProvince.class); 
     criteria.createAlias("country", "country"); 
     criteria.setCacheable(true); 
     criteria.add(Restrictions.eq(deleteFlag", false)); 
     criteria.add(Restrictions.eq("country", country)); 
     criteria.add(Restrictions.eq("country.deleteFlag", false)); 
     return criteria.list(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     session.getTransaction().commit(); 
     session.close(); 
    } 
} 
+0

我得到的答案請檢查它...我得到了我期望的輸出 –

+0

在你的代碼中,它只檢查國家級的刪除flage stateprovince。但它會檢索與該國家相對應的所有已刪除的瑕疵狀態。 –

+0

我認爲您需要詳細說明規格。 只要照顧這一點。 – SyntaX

0

公共類StateProvinceDAOImpl實現StateProvinceDAO {

@SuppressWarnings("unchecked") 
@Override 
public List<StateProvince> getAllState(Country country)throws HibernateException,ConstraintViolationException { 
    Session session = SessionFactoryUtil.getSessionFactory().openSession(); 
    try { 
     session.beginTransaction(); 
     Criteria criteria = session.createCriteria(StateProvince.class,"state"); 
     criteria.setCacheable(true); 
     criteria.createAlias("state.country", "country"); 
     criteria.add(Restrictions.eqProperty("state.deleteFlag", "country.deleteFlag")); 
     criteria.add(Restrictions.eq("country", country)); 
     criteria.add(Restrictions.eq("deleteFlag", false)); 
     return criteria.list(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     session.getTransaction().commit(); 
     session.close(); 
    } 
} 
+0

這不會工作,除非你指定'country.deleteFlag'! 你確定你的代碼運行沒有錯誤嗎? – SyntaX

+0

是它正確運行。我通過像http:// localhost:8080/provhrmapp/service/State?countryId = 11這樣的網址傳遞國家/地區 –