如何从方法前后带有参数的函数在方言上注册函数?

How to register a function on Dialect from a function with parameters before and after the method?

我正在尝试使用 MySQL 5.7 数据库中的 REGEXP 函数在 H2 内存数据库上创建 JPQL 运行。我想使用相同的查询进行集成测试。

由于 H2 上不存在 REGEXP 函数,我正在尝试注册一个新函数以使其工作,改为使用 REGEXP_LIKE H2 函数(仅用于测试)。

我的查询是:

String sql = "select o1.id from order o1 where :url REGEXP o1.regex";

但我想不出注册函数的正确语法。我在下面尝试这样的事情,但我知道无论如何这是不正确的,因为我发现的所有示例都是映射 "normal" 带参数的函数,但是 REGEXP 使用 :url REGEXP o1.regex 之类的语法而不是 REGEXP(:url, o1.regex):

public class MyH2Dialect extends H2Dialect {

    public MyH2Dialect() {
        super();
        registerFunction("REGEXP", 
            new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "REGEXP_LIKE(?1, ?2, 'i')"));
    }

}

JPQL 自然无法识别函数:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: REGEXP

我想象我需要做这样的事情(伪代码):

registerFunction("?1 REGEXP ?2", 
    new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "REGEXP_LIKE(?1, ?2, 'i')"));

有什么想法吗?

I'm trying to make a JPQL run using the REGEXP function from MySQL 5.7 database on a H2 memory database.

好吧,我无法注册该功能,但我能够 运行 H2 上的 REGEXP 功能。

我的错误是试图使用 JPQL 而不是 Native Query 来实现 运行。

em.createNativeQuery(sql);

当我 运行 在 H2 数据库的 MySQL 上使用相同的查询时,SQL 按预期工作。所以,我不需要注册这个功能。我无法理解这是可能的,因为 H2 文档 does not mention the REGEXP function but another question on SO 提到该函数存在。

我发现的另一种可能性(在使上述解决方案起作用之前)是将 MySQL 升级到 8.x 版本,因为它们创建了一个名为 REGEXP_LIKE 的新函数,几乎具有来自 H2.

REGEXP_LIKE 函数的相同签名