m2o*_*m2o 5 java postgresql jpa jpa-2.0
我使用 JPA 2.0 标准构建以下查询(简化):
select n from notif n where n.message ~ 'b.*la'
Run Code Online (Sandbox Code Playgroud)
我正在使用 postgresql 数据库,我真的需要 ~ 运算符,而不是像。我可以使用与 CriteriaBuilder.function 等效的东西吗?或者,postgres 中是否有 ~ 运算符的函数形式,以便我可以使用提到的 cb.function 方法。我只找到了 postgresql regexp_matches 函数,但它返回一个匹配数组而不是布尔值。
解决方案:由于从标准 API 迁移到 JPQL 是不可能的,所以我最终编写了一个 postgres 函数:
'CREATE OR REPLACE FUNCTION "regexp_search"(character varying,character varying) RETURNS boolean AS \'select $1 ~ $2;\' LANGUAGE sql;'
Run Code Online (Sandbox Code Playgroud)
并用 cb.function 调用它:
Expression<Boolean> regexp_search = cb.function("regexp_search", Boolean.class, message,cb.literal(re));
Run Code Online (Sandbox Code Playgroud)
REGEXP 或 ~ 不是 JPQL 标准的一部分。您可以使用本机 SQL 查询。
如果使用 EclipseLink,则支持 REGEXP,并且可以在 Postgres 上运行,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_regexp.htm#regexp
您还可以使用 SQL 函数调用 JPQL 内的任何 SQL 特定语法,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_sql.htm#sql
| 归档时间: |
|
| 查看次数: |
4217 次 |
| 最近记录: |