queryDSL 中的正则表达式匹配

Regular expression matching in queryDSL

我正在尝试进行查询,以便在 SQL 中重现以下内容:

where REGEXP_LIKE(variable, regular_expression);

我尝试使用匹配谓词和 MATCH 运算符,但似乎在这两种情况下,表达式都被转换为类似 com.mysema.query.types.ExpressionUtils#regexToLike.

regexToLike 方法会拒绝与正则表达式语法对应的任何“*”或“[”,并会抛出 QueryException :

'12-*34' can't be converted to like form"

有什么方法可以在 QueryDSL 上做到这一点吗?

编辑: 数据库是Oracle 11G

使用的querydsl库是:com.mysema.querydsl版本3.6.9

我将在迁移到 com.querydsl 的最新版本 (4) 后重试,因为 spring 数据 1.12 已发布(querydsl 4 支持).

相关代码如下:

String telRegex = "";
            if (this.tel.length() > 1)
            {
                char[] chars = this.tel.toCharArray();
                telRegex += chars[0];
                for (int i = 1; i < chars.length; i++)
                {
                    // NB can't make regex to work as special characters are banned cf. ExpressionUtils.regexToLike
                    //telRegex += "-*" + chars[i];
                    telRegex += "" + chars[i];
                }
            }
            else
            {
                telRegex = rechercheDto.getTelephone();
            }

            telRegex = "^" + telRegex + "$";

            // query
            predicate.and($(contact.getTel()).isNotNull())
            .and(
                $(contact.getTel()).matches(telRegex )
            );

JPA 查询中的模式匹配使用 LIKE 完成并且仅限于 %_。查询 DSL 将尝试将您的正则表达式转换为 LIKE 表达式。

来自 StringExpression.matches(String)

上的文档

Some implementations such as Querydsl JPA will try to convert a regex expression into like form and will throw an Exception when this fails

要使用 REXGEP 表达式执行 WHERE 子句,我使用以下代码

jpaQuery 
  = jpaQuery
      .where
        (Expressions.booleanTemplate
          ("function('REGEXP_LIKE',{0},{1}"
          ,qTable.ctcTireLineName
          ,sRegex
          )
        );

我使用 QueryDSL 3.7.2。

我刚刚尝试使用 sRegex = "^(EA|SPORT).*",它在 Java 8.

中运行良好