英文:
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 <String (category name), Long (amount of expenses for this category)>
did not occur:
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 notnull\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;
}
Now, using JPA, I have a problem with returning the 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 notnull\n" + "group by c.category_name;").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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论