如何在JPQL中应用正则表达式? [英] How to apply regular expression in JPQL?
问题描述
我正在使用JPA(休眠)作为持久层.
I'm using JPA (Hibernate) as the persistence layer.
我需要基于正则表达式添加WHERE子句, 这样的一些模式是SELECT * FROM TableName where REGEXP_LIKE(ColumnName,'Pattern').从结果中得到的是字符串列表,但是我需要从数据库中获取映射的实体作为对象而不是字符串.
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.
据我所知,JPQL可以将结果作为对象返回,但是JPQL似乎不支持正则表达式,因为它是Oracle的专有扩展.
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.
如何将正则表达式应用于JPQL?我还需要了解什么?
How can I apply regular expression to the JPQL?, what else should I need to know?
推荐答案
JPQL中没有完整的正则表达式,但是有模式值.根据规范 JPQL 2.2(p.188):
There are no full regular expressions in JPQL but there are pattern values. According to the specification JPQL 2.2 (p. 188):
pattern_value是字符串文字或字符串值输入参数,其中下划线(_)代表任何单个字符,百分号(%)字符代表任何字符序列(包括空序列),而所有其他角色代表自己.
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.
[...]
示例:
-
address.phone像"12%3"一样,对于"123","12993"为true,对于"1234"为false
address.phone LIKE ‘12%3’ is true for ‘123’ ‘12993’ and false for ‘1234’
asentence.word像"l_se"一样对"lose"是正确的,而对于"loose"则为false
asentence.word LIKE ‘l_se’ is true for ‘lose’ and false for ‘loose’
aword.加下划线的类似"_%" ESCAPE"\",对于"_foo"为true,对于"bar"为false
aword.underscored LIKE ‘_%’ ESCAPE ‘\’ is true for ‘_foo’ and false for ‘bar’
address.phone与"123%3"类似,对于"123"和"12993"为假,对于"1234"为true
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.
这篇关于如何在JPQL中应用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!