0

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.

Alex
  • 483
  • 1
  • 12
  • 23
  • What's your question? – JB Nizet May 27 '17 at 15:33
  • i want to query Empoyee table given Job desc property. How i do it? Currently my code returns empty list. – Alex May 27 '17 at 15:35
  • Your code doesn't do that at all. It looks for employees by name. So, why did you post this unrelated code? How about just using a JPQL query: `select e from Employee e where e.job.desc = :description`? – JB Nizet May 27 '17 at 15:38
  • cheers, but i want to return result as a Page class type list. Not just load all data into a list but load in chuncks. The reason is done because I have paginator, all these supportedby spring data – Alex May 27 '17 at 15:40
  • So? Spring data JPA supports paginated queries. Just annotate your method with @Query, and add a Pageable argument to the method. – JB Nizet May 27 '17 at 15:49
  • can you show how it might looks like. Is it the same findAll method from spring JpaRepository interface? – Alex May 27 '17 at 15:58

2 Answers2

1

Sorry dont have the reputation to comment so posting this as answer instead

As @JB Nizet suggested add a @Query to your method in your JPA Repository i.e employeeRepository

for example define a method like (method name can be anything)

@Query(value = "select e from Employee e where e.job.desc = :desc")
Page<Employee> findByDesc(Pageable pageable, @Param("desc") String desc);
Ranjeet
  • 859
  • 7
  • 19
  • ok, thanks @Dark. Will it actually work if I query to return second page of 10 items? – Alex May 27 '17 at 19:24
  • also, i see this unflexible. If I want to query by more than one property of both Employee and Job entity, this results in quite lengthy query. – Alex May 27 '17 at 20:27
0

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
Alex
  • 483
  • 1
  • 12
  • 23