[英]Error with query in java that works under database directly
I have this POSTGRESQL query:我有这个 POSTGRESQL 查询:
select distinct on (a.id) a.id, p.app
from application a inner join package p on a.app_id = p.app_id
where p.status = 'r'
ORDER BY a.app_id, (regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[1]::integer DESC,
(regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[2]::integer DESC,
(regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[3]::integer DESC
And this seems to work perfectly when I run in on the database directly.当我直接在数据库上运行时,这似乎工作得很好。 (Its returning all the packages with latest version) But when I call this query from Java method I get the error And I see from logs that the error is: ERROR: syntax error at or near ":" at character 593 and it seems that this created query translates to
(它返回所有最新版本的包)但是当我从 Java 方法调用这个查询时,我得到了错误而且我从日志中看到错误是:错误:字符 593 处或附近的语法错误似乎这个创建的查询转换为
select distinct on (a.id) a.id, p.app
from application a inner join package p on a.app_id = p.app_id
where p.status = 'r'
ORDER BY a.app_id, (regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[1]:integer DESC,
(regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[2]:integer DESC,
(regexp_matches(p.app_ver, '^(\d+)\.(\d+)\.(\d+)'))[3]:integer DESC
Like its missing one: .就像它缺少的一个:。 The way I pass this query is:
我传递此查询的方式是:
String query = BASE_QUERY +
"WHERE p.status = :status ORDER BY a.app_id, (regexp_matches(p.app_ver, '^(\\d+)\\.(\\d+)\\.(\\d+)'))[1]::integer DESC,\n" +
" (regexp_matches(p.app_ver, '^(\\d+)\\.(\\d+)\\.(\\d+)'))[2]::integer DESC,\n" +
" (regexp_matches(p.app_ver, '^(\\d+)\\.(\\d+)\\.(\\d+)'))[3]::integer DESC";
List<Application> fullList = entityManager.createNativeQuery(appListQuery, "Application")
.setParameter("status", PACKAGE_READY.getValue())
.getResultList();
Also another ERROR from log is the: Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet日志中的另一个错误是: Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Data from table colum app_ver is like:来自表列 app_ver 的数据如下:
21.9.0-3-dev
21.9.0-3-dev-03
21.9.0-6
3.0.13-1
3.0.13-1
3.0.13-1
3.0.13-1
21.9.0-2
21.9.0-2
3.0.13-1
21.9.0-2
21.9.0-2
21.9.0-144
3.0.13-1
21.9.0-2
21.9.0-4-devtest
21.9.0-170
21.9.0-170
21.9.0-170
21.9.0-170
21.9.0-170
21.9.0-170
21.9.0-170
21.9.0-170
What could be wrong with this?这可能有什么问题?
I'm not sure of the details but it seems that hibernate does some processing of your query, probably the :
is some special char for it我不确定细节,但似乎 hibernate 对你的查询做了一些处理,可能
:
是一些特殊的字符
did you tried to escape it?你试图逃避它吗? How to escape colon `:` within a native SQL query in Hibernate?
如何在 Hibernate 的本机 SQL 查询中转义冒号 `:`?
or put 4 of them given that when you use two in the query being run they become just 1?或者放 4 个,因为当您在正在运行的查询中使用两个时,它们只变成 1 个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.