使用HQL代替以下方法中的条件

时间:2014-10-04 08:50:28

标签: hibernate hql criteria

我有以下方法名为findExcludedFlagForfvAndij,并且我已经使用了标准,请告知我如何更改标准以便我可以使用hql代替标准,请告知hql将是什么

 public abfsm findExcludedFlagForfvAndij(IOAfv fv,ijType ij , String sx){       
            abfsm ioafvAndij=null;

        if (fv!=null && ij!=null){

        //****** want to use HQL instead of criteria******
        Criteria criteria = session.createCriteria(abfsm.class);
        criteria.add(Restrictions.eq("fvISOCode", fv.getSdmfvId()));

        criteria.add(Restrictions.or(Restrictions.isNull("sourceSystem" ), 
                  Restrictions.eq("sourceSystem", sx)));

        criteria.add(Restrictions.eq("ijCode", ij.getCode()));
        criteria.setFirstResult(0);
        criteria.setMaxResults(1);
        ioafvAndij = (abfsm) criteria.list();
        }       

        return ioafvAndij;
            }

1 个答案:

答案 0 :(得分:0)

对于Criteria代码:

    Criteria criteria = session.createCriteria(abfsm.class);
    criteria.add(Restrictions.eq("fvISOCode", fv.getSdmfvId()));

    criteria.add(Restrictions.or(Restrictions.isNull("sourceSystem" ), 
              Restrictions.eq("sourceSystem", sx)));

    criteria.add(Restrictions.eq("ijCode", ij.getCode()));

您可以使用以下HQL代码:

String hql="from abfsm where fvISOCode = :fvISOCode and ijCode = :ijCode and (sourceSystem IS NULL or sourceSystem = :sourceSystem)";     

Query query= session.createQuery(hql)
    .setParameter("fvISOCode", fv.getSdmfvId())
    .setParameter("ijCode", ij.getCode())
    .setParameter("sourceSystem", sx);