简体   繁体   中英

JPA query result is null

Hi I have the following code:

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

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

    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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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