3

I am trying to build an API which can search by HQL regex keywords,

EDITED: The best way to perform regex search in HQL is to use criteria, Restrictions.like() or Restrictions.ilike().

public static List<Object> createQueryAnd(Criteria cri,
        ArrayList<Parameters> list) {

    for (Parameters p : list) {
        String value = (String) p.value;
        if (value.contains("*")) {
            value = value.replace("*", "%");
        } else {
            value += "%";
        }
        Criterion c1 = Restrictions.ilike(p.property, value);
        cri.add(c1);

    }

    return cri.list();
}

Hope this helps someone

2
  • Y is this being voted down? was it a silly question? Commented Jan 9, 2013 at 20:07
  • It helped me, thank you! Vote up for you =) Commented Jun 20, 2013 at 12:26

1 Answer 1

5

HQL does not have regular expressions. If you want to use database provider specific constructs for regular expression, Dialect should be modified. This question contains discussion about how to do that with Oracle database.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any thing specific to postgres dialect?
You have to depend on functions/operators available in PostgreSQL when extending HQL: postgresql.org/docs/9.2/static/functions-matching.html
Thanks...Does this seem to work: SELECT * from table where regexp_matches(colname, 'lan*'); ?
No. As said, there is no support for regular expressions in HQL and also not in Hibernate criteria queries. To make it work, you have to implement it (part of implementation being extending Dialect). Also your query seems to be rather SQL than HQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.