How do I apply a regular expression in JPQL?

I am using JPA (Hibernate) as persistence layer.

I need to add a WHERE clause based on regex, pattern like this is SELECT * FROM TableName, where REGEXP_LIKE (ColumnName, 'Pattern'). What I get from the result is a list of strings, but I need to display the objects from the DB as an object, not a string.

From my knowledge JPQL can return the result as an object, but JPQL doesn't seem to support regex as it is a proprietary extension from Oracle.

How can I apply regex to JPQL? What else do I need to know?

+3


source to share


1 answer


JPQL doesn't have full regular expressions, but there are pattern values. According to JPQL 2.2 specification (p. 188) :

Pattern_value is a string literal or input parameter with string values, where the underscore (_) denotes any single character, the percent character (%) denotes any sequence of characters (including the empty sequence), and all other characters stand for themselves.

[...]

Examples:

  • address.phone LIKE '12% 3 is true for '123' 12993 and false for '1234

  • asentence.word LIKE 'l_se true for "lose and false for" loose

  • aword.underscored LIKE '_% ESCAPE' \ true for string "_foo and false for"

  • address.phone NOT LIKE '12% 3 is false for '123 and' 12993 and true for '1234



If you need more complex regex constructs, you need to use your own queries.

0


source







All Articles