I have an Employee entity class with Job entity mapped. In a controller I want to return all employees who have the job with the given Job description. How i do it? Currently my code returns empty list.
here is my code
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);
}
}
Thanks for your suggestions.
I have resolved the issue. The query by example worked fine it was just one the entity properties was could not be null so I had to add to ignore it.
.withIgnorePaths("job.property") <-- ignore property which is not null