Hibernate Criteria API中的LIKE限制 [英] LIKE restriction from the Hibernate Criteria API

查看:113
本文介绍了Hibernate Criteria API中的LIKE限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Hibernate的限制条件查询PostgreSQL数据库 like() 并带有部分关键字:

I am trying to query PostgreSQL database using Hibernate's restriction criterion like() with a partial keyword:

Criterion c1 = Restrictions.like("stateName", "Virg*");
cri.add(c1);

return cri.list();

除了 Restrictions.like( stateName, Virginia); 返回正确的记录。如何在Hibernate中使用部分 like 限制?

It doesn't return anything but Restrictions.like("stateName", "Virginia"); returns the correct record. How do I use partial like restrictions in Hibernate?

编辑:

通过执行以下操作使其起作用:


Got it working by doing something like this:

public static List<Object> createQueryStringByRegex(Criteria cri, Parameters p) {
    String value = (String) p.value;
    if (value.contains("*")) {
        value = value.replace("*", "%");
    } else {
        value += "%";
    }
    // System.out.println("Value: "+value);
    Criterion c1 = Restrictions.ilike(p.property, value);
    cri.add(c1);

    return cri.list();

}


推荐答案

使用列举 MatchMode 可以帮助您:

Criterion c1 = Restrictions.like("stateName", "Virg", MatchMode.START);

请勿使用任何特殊字符,例如*。而且,如果您希望使用不区分大小写的格式,请使用 ilike

Don't use any special character, like *. And if you want a case-insensitive like, use ilike.

这篇关于Hibernate Criteria API中的LIKE限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆