Postgresql array_agg 多列 JDBC

Postgresql array_agg of multiple columns with JDBC

我正在尝试加入一个 table,它可能有多个给定 ID 的条目,并将与该 ID 对应的行聚合到一个数组中。这在 SQL 查询中如下所示:

SELECT * from data
LEFT JOIN (select id, array_agg(row(foo, bar)) AS foo_bar_data from foo_bar_table group by id) AS temp using(id)

这按预期工作,但我无法读取 JDBC 中的结果。

ResultSet rs = st.executeQuery(...)
Array a = rs.getArray("foo_bar_data")
// Now I want to iterate over the array, reading the values foo and bar of each item.

到目前为止,我的努力总是以 Method org.postgresql.jdbc4.Jdbc4Array.getArrayImpl(long,int,Map) is not yet implemented. 异常结束。我如何遍历 a,检索值 foobar?

编辑:我还应该提一下,foobar 的类型不同。

Postgres JDBC 驱动程序不支持除基本类型(数字、date/timestamp、字符串)作为 JDBC 数组之外的任何内容。您可以调用 array_agg 两次并在每一行上获得两个数组:

    try (Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "postgres");
                 ResultSet rs = db.createStatement().executeQuery("select array_agg(i), array_agg(s) from (select 1 i, 'a' s union select 2 i, 'b' s) t")) {
        rs.next();
        System.out.println(Arrays.toString((Object[]) rs.getArray(1).getArray()));
        System.out.println(Arrays.toString((Object[]) rs.getArray(2).getArray()));
    }