【发布时间】:2019-07-22 19:58:23
【问题描述】:
各位程序员,你好,我想在循环中添加多个列表到 HashMap 中。我不知道为什么当它到达第二次迭代记录时会成倍增加,例如二月有 10 条记录(monthId = 2),在整个循环之后有 40 条记录被注入。下面是代码:
public HashMap<String,List<Transaction>> convertTransactionsPerMonth(int
userId){
for(int monthId = 1; monthId < 13; monthId++){
ArrayList<Transaction> transactionsFromDatabase = new ArrayList<>
(entityManager
.createQuery("SELECT t FROM Transaction t WHERE
MONTH(t.transactionDate) LIKE :monthId AND t.user.id = :userId",
Transaction.class)
.setParameter("monthId", monthId)
.setParameter("userId", userId)
.getResultList());
transactionsPerMonth.put(Months.getById(monthId),
transactionsFromDatabase);
}
return transactionsPerMonth;
}
【问题讨论】:
标签: java spring hibernate jpql