regex - 如何在JPQL中应用正则表达式?

标签 regex hibernate jpql

我使用 JPA (Hibernate) 作为持久层。

我需要添加一个基于正则表达式的 WHERE 子句, 这样的一些模式是 SELECT * FROM TableName where REGEXP_LIKE(ColumnName, 'Pattern')。我从结果中得到的是字符串列表,但我需要从数据库中获取映射实体作为对象而不是字符串。

据我所知,JPQL 可以将结果作为对象返回,但 JPQL 似乎不支持正则表达式,因为它是 Oracle 的专有扩展。

如何将正则表达式应用于 JPQL?我还需要了解什么?

最佳答案

JPQL 中没有完整的正则表达式,但有模式值。根据规范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’

如果您想要更高级的正则表达式构造,您需要使用 native 查询。

关于regex - 如何在JPQL中应用正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9733667/

相关文章:

java - Spring Boot Hibernate+Mysql问题

regex - 正则表达式组匹配和替换在单个语句中

php - 如何在PHP中选择以某个单词开头的文件?

正则表达式中的 Python 正则表达式否定

java - 如何使用@OneToMany 集合进行分页

java - 如何使用jsf管理表单上的多对多关系

java - 使用 Spring Data JPA 进行搜索

java - 如何处理 Spring data jpa 和 jpql 查询中非必需参数的 PSQLException upper(bytea) 问题

java - 使用 IN 子句更新 JPQL 中的用户实体字段

javascript - 用于验证最后一位数字的正则表达式