将休眠或条件置于标准中

时间:2014-09-30 04:39:51

标签: hibernate criteria

你能不能建议我需要把或者条件放在hibernate标准中作为参数之一..我现在得到一个参数abcsystem现在它的值也可以为null,或者它也不能为null但是如果它值不是null然后设置它的值,因为它是参数abcystem原样,请建议如何实现这一点......

    Criteria criteria = session.createCriteria(ils.class);
    criteria.add(Restrictions.eq("Code", ok.getId()));
    //****criteria.add(Restrictions.eq("abcystem", ok.getabcSystem())); // ****criteria to be modified such that abc system can be null or //****we need to set the value of abc system whatever is coming in ok.getabcSystem() in the paremeter abcystem
    criteria.add(Restrictions.
    criteria.add(Restrictions.eq("tCode", ok.tCode()));

ment = (ils) criteria.uniqueResult();

2 个答案:

答案 0 :(得分:1)

如果我正确地阅读了您的要求,我们必须做的是评估ok.getabcSystem()的价值并且:

  • 使用如果 null
  • 如果
  • ,请使用它

如果这是我们需要的,那么评估不应该是数据库引擎,我们可以在服务器/ app运行时执行:

Criteria criteria = session.createCriteria(ils.class);
criteria.add(Restrictions.eq("Code", ok.getId()));

if(ok.getabcSystem() != null) {
    criteria.add(Restrictions.eq("abcystem", ok.getabcSystem()))
}
...

答案 1 :(得分:0)

试试这个

  Criteria criteria = session.createCriteria(ils.class);
  criteria.add(Restrictions.eq("Code", ok.getId()));
  // OR condition 
  criteria.add( Restrictions.or(
        Restrictions.eq( "abcystem", ok.getabcSystem()),
        Restrictions.isNull("abcystem")
    ));
  criteria.add(Restrictions.eq("tCode", ok.tCode()));
 // your result here  ment = (ils) criteria.uniqueResult();