结果集为空,但查询在 Postgres 中有效

Result set is empty but query works in Postgres

我正在尝试从 Postgres 获取一些结果到我的 Java 应用程序中。截至目前,我总是使用 PreparedStatements 和 ResultSets 从内存中的数据库中获取数据。但是今天我的代码拒绝工作,尽管我使用的代码和往常一样。这是我的一段代码:

PreparedStatement stmtExplicitCollections = this.originalGraph.getPreparedStatement(
    "(" +
    "SELECT ce1.collId " +
    "FROM quotient_summary_edges qse, collection_to_ec ce1, collection_to_ec ce2 " +
    "WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget = ce2.ecId " +
    "GROUP BY ce1.collId " +
    "HAVING COUNT(ce2.collId) = 1" +
    ") INTERSECT (" +
    "SELECT ce1.collId " +
    "FROM quotient_summary_edges qse, collection_to_ec ce1 " +
    "WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget IN (" + StringUtils.join(interestingEquivalenceClasses, ",") + ")" +
    ");");
log.info(stmtExplicitCollections.toString());
ResultSet rsExplicitCollections = stmtExplicitCollections.executeQuery();
ArrayList<Integer> explicitCollectionsIds = new ArrayList<>();
while (rsExplicitCollections.next()) {
    explicitCollectionsIds.add(rsExplicitCollections.getInt(1));
}
log.info(explicitCollectionsIds);

这是日志:

2022-03-29 13:35:03 INFO  RecordsCollectionsComputation:307 - (SELECT ce1.collId FROM quotient_summary_edges qse, collection_to_ec ce1, collection_to_ec ce2 WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget = ce2.ecId GROUP BY ce1.collId HAVING COUNT(ce2.collId) = 1) INTERSECT (SELECT ce1.collId FROM quotient_summary_edges qse, collection_to_ec ce1 WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget IN (4,0,1,5))
2022-03-29 13:35:03 INFO  RecordsCollectionsComputation:313 - []

ArrayList explicitCollectionsIds 已 empty/not 已填充且 if(rsExplicitCollections.next()) returns 已填充。

编辑: 此外,查询在 Postgres 中有效,如下所示:

cl_xml=# (SELECT ce1.collId FROM quotient_summary_edges qse, collection_to_ec ce1, collection_to_ec ce2 WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget = ce2.ecId GROUP BY ce1.collId HAVING COUNT(ce2.collId) = 1) INTERSECT (SELECT ce1.collId FROM quotient_summary_edges qse, collection_to_ec ce1 WHERE qse.summaryNodeIdSource = ce1.ecId AND qse.summaryNodeIdTarget IN (4,0,1,5));

 collid
--------
      0
      1
(2 rows)

有人可以帮助我吗?提前致谢!

在我看来,您的应用程序似乎触发了一个查询 SELECT id FROM table, 然后收到一个空的结果集。没有抛出异常。

这意味着与数据库的通信正常,查询执行正常,但就是找不到数据。

停止查看代码或网络中的更改。开始查看您的数据库内容。表格里有什么?

感谢@Antoniosss,我终于找到了问题!问题是我正在处理未提交的更改,即 table collection_to_ec 在我执行查询时未填充(稍后填充)