我有一个映射了 Job 实体的 Employee 实体类。在控制器中,我想返回所有具有给定职位描述的职位的员工。我怎么做?目前我的代码返回空列表。
这是我的代码
public class Employee {
public int id;
public String name;
@ManyToOne()
@JoinColumn(name="JOB_ID", referencedColumnName="JOB_ID")
public Job job;
...
}
public class Job {
public int jobId;
public String desc; // job description
...
}
@Controller
public MyController {
// I want to filter employees when given Job desc property
@RequestMapping("/filter")
public ModelAndView filterBy(
@ModelAttribute("emp") Employee emp,
Pageable pageable, ModelMap model) {
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("name", match -> match.startsWith().ignoreCase());
Example<Employee> example = Example.of(emp, matcher);
Page<Employee> employeePage = employeeRepository.findAll(emp,
new PageRequest(pageable.getPageNumber(),
pageable.getPageSize(), null));
PageWrapper<Employee> page = new PageWrapper<Employee>(employeePage, "/employee");
model.addAttribute("employee", empPage.getContent());
model.addAttribute("page", page);
return new ModelAndView("employeePage", model);
}
}
感谢您的建议。