java - 使用 ltree 本机命名查询休眠
问题描述
我正在尝试运行包含ltree
函数和运算符的 PostgreSQL 本机查询。
这是定义:
@NamedNativeQuery(
name = "pathSegmentQuery",
query = "select ltree2text(okm_path) as okm_path, " +
" index(okm_path, text2ltree(:lastSegment)) + 2 <> nlevel(okm_path) as haschild, " +
" case " +
" when index(okm_path, text2ltree(:lastSegment)) + 1 <> nlevel(okm_path) " +
" then ltree2text(subpath(okm_path, index(okm_path, text2ltree(:lastSegment)) + 1, 1)) " +
" end as child " +
"from document " +
"where okm_path ~ :pathLike " +
"and " +
"index(okm_path, text2ltree(:path)) + 1 <> nlevel(okm_path) ",
resultSetMapping = "pathSegmentQueryRSMapping")
像这样调用:
public List<PathSegment> getPathChildren(String path, String lastSegment) {
Query query = entityManager.createNamedQuery("pathSegmentQuery");
String pathLike = "'*." + path + ".*'";
query.setParameter("path", path);
query.setParameter("pathLike", pathLike);
query.setParameter("lastSegment", lastSegment);
return query.getResultList();
}
结果是错误ERROR: operator does not exist: ltree ~ character varying
。
当我尝试直接针对数据库运行查询时,它运行正常:
select ltree2text(okm_path) as okm_path,
index(okm_path, text2ltree('_root_')) + 2 <> nlevel(okm_path) as haschild,
case
when index(okm_path, text2ltree('_root_')) + 1 <> nlevel(okm_path)
then ltree2text(subpath(okm_path, index(okm_path, text2ltree('_root_')) + 1, 1))
end as child
from document
where
okm_path ~ '*._root_.*'
and
index(okm_path, text2ltree('_root_')) + 1 <> nlevel(okm_path)
从错误中可以明显看出 hibernate(?) 不喜欢 th 运算符右侧的类型~
,但是正如您所看到的,我在后面的查询中使用了该字符串并且工作正常。
那么我需要对休眠查询做什么才能成功运行查询呢?
编辑:当我替换我时okm_path ~ :pathLike
,"where okm_path ~ '*._root_.*' "
我会得到:
org.postgresql.util.PSQLException: ERROR: syntax error at position 0
错误
休眠:5.2.9.Final
PostgreSQL:9.2.23
解决方案
事实证明,lquery()
当您对 lquery 进行操作时,需要调用某个函数。
所以我的查询转化为
...
where okm_path ~ lquery(:pathLike)
...
这解决了问题
推荐阅读
- c - 在 c 中强制转换会导致任何编译错误?
- css - 无法在 webpack 4 中加载 css 文件
- php - 在 PHP 中,函数体如何在其动态父级中引用变量?
- macos - Mac上的SKCameraNode定位:非视网膜vs视网膜
- java - 使用 java 错误将十进制转换为二进制
- selenium - 使用 DrJava IDE 运行 selenium 测试
- amazon-lex - NLU - 提取与实体相关的字符串
- tcl - 有没有办法用一个命令创建一个嵌套的字典?
- java - 通过 onmapclick 在谷歌地图上添加标记
- php - 即使单击其他图像,我的 jquery 函数也仅显示第一张图像我希望在单击该图像时显示正确的图像