2015-11-11 27 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这样的网址传递国家/地区 –