How to apply regular expression in JPQL?

一世执手 提交于 2019-12-10 18:40:21

问题


I'm using JPA (Hibernate) as the persistence layer.

I need to add a WHERE clause based on a regular expression, such some pattern are SELECT * FROM TableName where REGEXP_LIKE(ColumnName, 'Pattern'). What I get from the result is the list of string but I need to get mapped entities 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 regular expressions, as it's a propietary extension from Oracle.

How can I apply regular expression to the JPQL?, what else should I need to know?


回答1:


There are no full regular expressions in JPQL but there are pattern values. According to the specification JPQL 2.2 (p. 188):

The pattern_value is a string literal or a string-valued input parameter in which an underscore (_) stands for any single character, a percent (%) character stands for 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’ is true for ‘lose’ and false for ‘loose’

  • aword.underscored LIKE ‘_%’ ESCAPE ‘\’ is true for ‘_foo’ and false for ‘bar’

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

If you want more advanced regular expression constructs you need to use native queries.



来源:https://stackoverflow.com/questions/9733667/how-to-apply-regular-expression-in-jpql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!