Spring+boot+data - 通过具有关联 return 页面的示例进行查询
Spring+boot+data - query by Example having association return Page
我有一个 Employee 实体 class 映射了 Job 实体。在控制器中,我想 return 所有拥有给定工作描述的工作的员工。我该怎么做?目前我的代码 return 是空列表。
这是我的代码
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);
}
}
感谢您的建议。
抱歉没有发表评论的名誉,所以将其发布为答案
正如@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);
我已经解决了这个问题。通过示例查询工作正常它只是一个实体属性不能为 null 所以我不得不添加以忽略它。
.withIgnorePaths("job.property") <-- ignore property which is not null
我有一个 Employee 实体 class 映射了 Job 实体。在控制器中,我想 return 所有拥有给定工作描述的工作的员工。我该怎么做?目前我的代码 return 是空列表。
这是我的代码
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);
}
}
感谢您的建议。
抱歉没有发表评论的名誉,所以将其发布为答案
正如@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);
我已经解决了这个问题。通过示例查询工作正常它只是一个实体属性不能为 null 所以我不得不添加以忽略它。
.withIgnorePaths("job.property") <-- ignore property which is not null