简体   繁体   English

直接在数据库下工作的java查询出错

[英]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.

相关问题 Java准备的语句查询返回错误,但是如果我将其打印并直接在数据库上执行,它将起作用 - java prepared statement query is returning an error but If I print it and execute on the database directly it works 数据库查询直接在DBMS上工作但不在Java中 - Database Query working on DBMS directly but not in Java Java程序查询数据库错误 - Java program to query database error Java 代码在从变量引用时有效,但在直接调用时抛出错误 - Java code works when referenced from a variable but throws an error when called directly 如何调试在Java 7下有效但在Java 8下不可行的内容 - How to debug something that works under Java 7 but not under Java 8 mySQL查询提供了Java中的语法错误,但在mySQL工作台中工作正常 - mySQL query gives syntax error in java, but works fine in mySQL workbench 有关SQLsyntax的Java错误,而查询在MySQL中可正常运行 - error in java about SQLsyntax while query works correctly in MySQL SQL查询在工作台中工作,但在Java中给出语法错误 - SQL Query works in workbench but gives syntax error in java Java EWS API可在Windows下运行,但不能在Linux上运行 - Java EWS API works under windows but not on linux Java AtomicReference如何在引擎盖下工作 - How java AtomicReference works under the hood
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM