添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I make a method of search using named query

@Query(value = "SELECT * FROM working_day w INNER JOIN \r\n"
            + "employees_days e ON \r\n"
            + "w.day_id = e.day_id\r\n"
            + "WHERE w.date LIKE %:date% AND e.employee_id LIKE %:employee_id%", nativeQuery = true)
    List<WorkingDay> searchDaysForMonth(@Param("date") String date, @Param("employee_id") Long employeeId);

Service:

public List<WorkingDay> countDays(String date, Long employeeId) {
    return workingDayRepository.searchDaysForMonth(date, employeeId);

Controller method:

@RequestMapping(value = "/employees")
@GetMapping(value = "/employees/days_in_month")
    public ModelAndView getDaysForEmployee(@RequestParam(value = "date", required = false) String date,
                                           @RequestParam(value = "employee_id") Long employeeId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("days_employees");
        var daysForEmployee = employeeService.countDays(date, employeeId);
        modelAndView.addObject("days_employees", daysForEmployee);
        Employee employee = employeeService.getById(employeeId);
        modelAndView.addObject("employee", employee);
        var totalDays = daysForEmployee.size();
        modelAndView.addObject("totalDays", totalDays);
        return modelAndView;

When I enter the following mapping value

http://localhost:8080/employees/days_in_month?date=2023-01&employee_id=2

I get MethodArgumentTypeMismatchException. Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long';

How to solve this?

It looks like your employee_id is an integer value - what are you hoping that a LIKE filter will achieve? – Jon Skeet Jan 26 at 14:31 Where are you calling searchDaysForMonth? You are not showing the call of that method. Or is the exception already thrown in/before the controller method? – knittl Jan 26 at 14:33 After I corrected my query it still doesn't work. @Query(value = "SELECT * FROM working_day w INNER JOIN \r\n" + "employees_days e ON \r\n" + "w.day_id = e.day_id\r\n" + "WHERE w.date LIKE %:date% AND e.employee_id = %:employee_id%", nativeQuery = true) – SealAnia Jan 26 at 14:47 "...e.employee_id = %:employee_id%" doesn't seem corrected. Do you realize that the %...% syntax is for the like operator? Why don't you use what Tim suggested: " ... e.employee_id = :employee_id"? – Thomas Jan 26 at 14:53

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.