0

我有一个映射了 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);
    }
}

感谢您的建议。

4

2 回答 2

1

抱歉,没有评论的声誉,因此将其发布为答案

正如@JB Nizet 建议的那样,在您的JPA 存储库中的方法中添加一个@Query,即employeeRepository

例如定义一个方法(方法名可以是任何东西)

@Query(value = "select e from Employee e where e.job.desc = :desc")
Page<Employee> findByDesc(Pageable pageable, @Param("desc") String desc);
于 2017-05-27T16:59:54.303 回答
0

我已经解决了这个问题。示例查询运行良好,它只是实体属性之一不能为空,因此我必须添加以忽略它。

.withIgnorePaths("job.property") <-- ignore property which is not null
于 2017-05-27T22:23:55.153 回答