【问题标题】:Like % operator in HQL像 HQL 中的 % 运算符
【发布时间】:2018-03-21 20:34:36
【问题描述】:

我正在尝试运行下面的代码,但出现错误。

public MdFeedHandOff getFeedByHandOff(Integer handoff, Integer csiID){
        logger.error("*************getFeedByHandOff(): handoff value is "+handoff);
        String hql = "FROM MdFeedHandOff a WHERE a.handoff = :handoff  and  a.active  in ('A','I') and to_char(a.handoff) like :csiID%";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("handoff", handoff);
        List<MdFeedHandOff> batchJobList = searchForList(hql, param);
        return batchJobList.isEmpty()?null:batchJobList.get(0);
    }

我想在 hql 中使用 like % 运算符。请帮助我。 我也尝试过以下行,但它不起作用。

String hql = "FROM MdFeedHandOff a WHERE a.handoff = :handoff  and  a.active  in ('A','I') and to_char(a.handoff) like :csiID" + "%";

【问题讨论】:

  • searchForList 里面有什么?如何创建查询并设置参数?
  • @SuppressWarnings("unchecked") public List searchForList(final String sentence, final Map parameters) { return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createQuery(sentence); query.setProperties(parameters); return query.list(); } }); }

标签: java hibernate jpa orm hql


【解决方案1】:

您应该将:csid 参数名称放入查询中,并在您设置的参数中添加 % 通配符。此外,您似乎没有在参数映射中设置 csid。

查询:

FROM MdFeedHandOff a WHERE a.handoff = :handoff 
     and  a.active  in ('A','I') and to_char(a.handoff) like :csiID";

参数:

Map<String, Object> param = new HashMap<String, Object>();
param.put("handoff", handoff);
param.put("csiID", csiId.toString()+"%");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多