【发布时间】:2021-10-15 15:04:42
【问题描述】:
我正在尝试将 OracleDB HQL where 子句转换为 PosgreSQL。 HQL 使用的是 PosgreSQL 方言。
这两个查询都直接在数据库上运行,没有任何问题。
Oracle数据库:
...
where
( LPAD(regexp_substr(someTable.someColumn, '\d+', 1,1), 3, ' ') = ' 40'
AND LPAD(regexp_substr(someTable.someColumn, '\d+', 1,2), 3, ' ') = ' 20');
PosgreSQL:
...
where
( LPAD(( SELECT array_to_string(a, '') from regexp_matches(someTable.someColumn, '\d+', 'g') as a limit 1 offset 0), 3, ' ') = ' 40'
AND LPAD(( SELECT array_to_string(a, '') from regexp_matches(someTable.someColumn, '\d+', 'g') as a limit 1 offset 1), 3, ' ') = ' 20' );
更改代码后,我在运行时一直低于 hql 异常:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: regexp_matches near line 1
我还注册了下面的函数自定义方言类,但效果不佳:
public class CustomPostgresqlDialect extends PostgreSQL82Dialect {
public CustomPostgresqlDialect() {
super();
registerFunction( "regexp_matches", new StandardSQLFunction( "regexp_matches", StandardBasicTypes.STRING ) );
}
}
如果我继续使用原始 regexp_substr 版本,则会出现 PostgreSQL 异常:
org.postgresql.util.PSQLException: ERROR: function regexp_substr(character varying, unknown, integer, integer) does not exist
请建议是否有办法转换此类查询,以便 HQL 不会解析它。 此外,我无法切换到原生查询创建,因为 HQL 生成的部分很大。
非常感谢
【问题讨论】:
-
我认为您的 Postgres 解决方案太复杂了。你到底想在那里实现什么?我认为
regexp_split_to_array()可能会更简单
标签: java postgresql oracle hibernate hql