0

i have a problem. I'm trying to do custom query with filtering by date usin time stamp, but im using Pageable and it adds to query after comma id desc limit and it causing error like below. I dont know how to get arroung this problem.

select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id LIMIT 999999, id desc limit ?
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']
2021-03-05 15:49:59.556  WARN 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
2021-03-05 15:49:59.556 ERROR 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc limit 300' at line 1

Custom query

    @Query(nativeQuery = true, value = "select * from lights Where timestamp BETWEEN :startDate AND :endDate ORDER BY id LIMIT 999999")
    Page<Lights> findBetweenDates(@Param("startDate") String startDate, @Param("endDate") String endDate, Pageable pageable);

Conrtoller class

    @GetMapping("/lights")
    public String viewHomePage(Model model, String startDate , String endDate) {
        startDate = "'2021-03-04 15:27:07'";
        endDate = "'2999-12-31 23:59:59'";
        return findPaginated(1, "id", "desc",startDate,endDate, model);
    }

    @RequestMapping({"/lights/page/{pageNo}"})
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,
                                @RequestParam("sortField" ) String sortField,
                                @RequestParam("sortDir") String sortDir,
                                @RequestParam(value = "startDate", defaultValue = "'2021-03-04 15:27:07'") String startDate,
                                @RequestParam(value = "endDate", defaultValue = "'2999-12-31 23:59:59'") String endDate,
                                Model model){
        int pageSize = 200;


        Page<Lights> page = lightsMapService.findPaginated(pageNo,pageSize,sortField,sortDir,startDate,endDate);
        List<Lights> lightsList = page.getContent();

        model.addAttribute("currentPage", pageNo );
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems",page.getTotalElements() );

        model.addAttribute("sortField", sortField );
        model.addAttribute("sortDir", sortDir );
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate", endDate);
        model.addAttribute("reverseSortDir", sortDir.equals("asc")?"desc":"asc");


        model.addAttribute("lightsList", lightsList );

        return "sensorsPages/lights";

    }

After i delete LIMIT 99999 from query i'm not getting error enymore, but got this and its not working:

2021-03-05 16:09:47.789 DEBUG 3540 --- [nio-8080-exec-7] org.hibernate.SQL                        : select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id, id desc limit ?
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']
wiler121
  • 1
  • 1
  • If you use a `NamedQuery` you should not have this issue. Which version of spring and hibernate you are using ? – Raghuveer Mar 05 '21 at 15:14
  • have you tried to execute the query manually? – Mahmoud Odeh Mar 05 '21 at 15:21
  • I resolved this when i added in my html correct th:href Id But its woking only for date without filtering by hours 2021-03-04 23:39:08 i think its caused by spaces in link query but i dont know how to parse it to form that will match with query statement localhost:8080/sensorsPages/lights/page/11?sortField=id&sortDir=asc&startDate=2021-03-04 23:58:12&endDate=2021-03-04 23:58:12 – wiler121 Mar 05 '21 at 15:45
  • I'm using spring boot starter's 2.4.3 – wiler121 Mar 05 '21 at 15:49

0 Answers0