简体   繁体   English

JPA 查询结果为 null

[英]JPA query result is null

Hi I have the following code:嗨,我有以下代码:

Controller: Controller:

@GetMapping("/salesTrend")
private List<Object[]> salesTrend(){
    int[] profit = {1,2,3,4,5,6,7,8,9,10,11,12};
    return customerRepository.findByProfit(profit);
}

Repository:存储库:

@Query(
    value = "SELECT MONTH(f_date), SUM(profit) as p from finance where MONTH(f_date) = ?1",
    nativeQuery = true)
List<Object[]> findByProfit(int[] profit);

the result of this is null and I am struggling to know why that is because I tested this already on xampp and it works fine I don't know what I'm doing wrong thank you结果是 null ,我很难知道为什么会这样,因为我已经在 xampp 上测试过它,它工作正常我不知道我做错了什么谢谢

I have solved it already thanks to the comment of @ppeterka I just made an array containing all the months then I looped each of the elements in the array to put them at the query one at a time like so由于@ppeterka 的评论,我已经解决了它我刚刚创建了一个包含所有月份的数组,然后我循环数组中的每个元素,一次将它们放在查询中

    int[] profit = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    List<Object> profitList = new ArrayList<>();
    
    for(int i = 1; i < profit.length; i++) {
        List<Object[]> results = customerRepository.findByProfit(i);
        for(Object[] obj : results) {
            Map<Object, Object> profits = new LinkedHashMap<>();
            profits.put("Month", obj[0]);
            profits.put("Profit", obj[1]);
            
            profitList.add(profits);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM