是否可以将createNativeQuery方法的结果从List转换为Map?

huangapple go评论74阅读模式
英文:

Is it possible to convert the result of the createNativeQuery method from List to Map?

问题

目前我有一个返回费用报告的方法。以前,当我使用 JDBC 时,以 Map <String (类别名称), Long (该类别的费用金额)> 的形式返回报告时并没有问题:

public Map<String, Long> expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
    Map<String, Long> map = new HashMap<>();
    try (Connection con = ds.getConnection()) {
        PreparedStatement ps = con.prepareStatement("SELECT c.category_name, sum(amount)\n" +
                "FROM account as a\n" +
                "left join transaction as t on account_from_id = account_id\n" +
                "left join transaction_to_transaction_type tttt on t.transaction_id = tttt.transaction_id\n" +
                "left join category c on tttt.transaction_type_id = c.category_id\n" +
                "WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name is not null\n" +
                "group by c.category_name;");
        ps.setObject(1, startDate);
        ps.setObject(2, endDate);
        ps.setLong(3, customerId);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            map.put(rs.getString("category_name"), rs.getLong("amount"));
        }
    } catch (SQLException e) {
        throw new CustomException(e);
    }
    return map;
}

现在,使用 JPA,我在返回 Map 方面遇到了问题:

public List expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
    return em.createNativeQuery("select c.category_name, sum(amount)\n" +
            "from account as a\n" + "left join transaction as t on account_from_id = account_id\n" +
            "left join transaction_to_category ttc on t.transaction_id = ttc.transaction_id\n" +
            "left join category c on ttc.category_id = c.category_id\n" +
            "WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name is not null\n" + "group by c.category_name;")
            .setParameter(1, customerId).setParameter(2, startDate).setParameter(3, endDate).getResultList();
}

如何解决这个问题?

英文:

At the moment I have a method that returns an expense report. Earlier, when I used JDBC, there were problems with returning a report in the form of Map &lt;String (category name), Long (amount of expenses for this category)&gt; did not occur:

public Map&lt;String, Long&gt; expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
        Map&lt;String, Long&gt; map = new HashMap&lt;&gt;();
        try (Connection con = ds.getConnection()) {
            PreparedStatement ps = con.prepareStatement(&quot;SELECT c.category_name, sum(amount)\n&quot; +
                    &quot;FROM account as a\n&quot; +
                    &quot;left join transaction as t on account_from_id = account_id\n&quot; +
                    &quot;left join transaction_to_transaction_type tttt on t.transaction_id = tttt.transaction_id\n&quot; +
                    &quot;left join category c on tttt.transaction_type_id = c.category_id\n&quot; +
                    &quot;WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name notnull\n&quot; +
                    &quot;group by c.category_name;&quot;);
            ps.setObject(1, startDate);
            ps.setObject(2, endDate);
            ps.setLong(3, customerId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                map.put(rs.getString(&quot;category_name&quot;), rs.getLong(&quot;amount&quot;));
            }
        } catch (SQLException e) {
            throw new CustomException(e);
        }
        return map;
    }

Now, using JPA, I have a problem with returning the Map:

public List expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
        return em.createNativeQuery(&quot;select c.category_name, sum(amount)\n&quot; +
                &quot;from account as a\n&quot; + &quot;left join transaction as t on account_from_id = account_id\n&quot; +
                &quot;left join transaction_to_category ttc on t.transaction_id = ttc.transaction_id\n&quot; +
                &quot;left join category c on ttc.category_id = c.category_id\n&quot; +
                &quot;WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name notnull\n&quot; + &quot;group by c.category_name;&quot;).setParameter(1, customerId).setParameter(2, startDate).setParameter(3, endDate).getResultList();
    }

How can this problem be solved?

答案1

得分: 1

请尝试以下代码:

public Map<String, Long> expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
    List<Object[]> rows = em.createNativeQuery("select c.category_name, sum(amount)\n" +
            "from account as a\n" + "left join transaction as t on account_from_id = account_id\n" +
            "left join transaction_to_category ttc on t.transaction_id = ttc.transaction_id\n" +
            "left join category c on ttc.category_id = c.category_id\n" +
            "WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name notnull\n" + "group by c.category_name;").setParameter(1, customerId).setParameter(2, startDate).setParameter(3, endDate).getResultList();

    Map<String, Long> retVal = new HashMap<>();

    for (Object[] row : rows) {
        retVal.put(row[0], row[1]);
    }
    return retVal;
}
英文:

Try this

public Map&lt;String, Long&gt; expenseReport(long customerId, LocalDate startDate, LocalDate endDate) {
        List&lt;Object[]&gt; rows = em.createNativeQuery(&quot;select c.category_name, sum(amount)\n&quot; +
                &quot;from account as a\n&quot; + &quot;left join transaction as t on account_from_id = account_id\n&quot; +
                &quot;left join transaction_to_category ttc on t.transaction_id = ttc.transaction_id\n&quot; +
                &quot;left join category c on ttc.category_id = c.category_id\n&quot; +
                &quot;WHERE (t.data_created BETWEEN ? AND ?) AND a.customer_id = ? AND category_name notnull\n&quot; + &quot;group by c.category_name;&quot;).setParameter(1, customerId).setParameter(2, startDate).setParameter(3, endDate).getResultList();

    Map&lt;String, Long&gt; retVal = new HashMap&lt;&gt;();

    for (Object[] row : rows) {
        retVal.put(row[0], row[1]);
    }
    return retVal;
}

huangapple
  • 本文由 发表于 2023年2月27日 04:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定