1
votes

I am facing issue in querying repository with find all by child object ID, Example and Pageable.

StudentController.java

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    
    @RequestMapping(method = RequestMethod.GET)
    public Page<Student> getStudentsPage(
            @PageableDefault(page = 0, size = 10) @SortDefault.SortDefaults({
                    @SortDefault(sort = "id", direction = Direction.DESC) }) Pageable pageable,
            Student student) {
        return studentService.getStudentPage(student, pageable);
    } 
}

StudentService.java

@Service
public class StudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentService.class);    
    @Autowired
    private UserService userService;
    
    @Autowired
    private StudentRepository studentRepository;
    
    public Page<Student> getStudentsPage(Student student, Pageable pageable) {
        logger.debug("Getting Students : {}, {}", student, pageable);
        //gets organization Id of the student from the session
        String organizationId = userService.getUserMe().getOrganization().getId(); 
        Page<Student> studentPage = studentRepository.findAllByOrganizationId(organizationId, Example.of(student), pageable);
        
        logger.debug("Students: {}", studentPage.getContent());
        return studentPage;
    }
}

StudentRepository.java

@Repository
public interface StudentRepository extends MongoRepository<Student, String> {    
    Page<Student> findAllByOrganizationId(String organizationId, Example<Student> example, Pageable pageable);
    
}

Student.java

@Document(collection = "students")
public class Student {
    @Id
    private String       id;
    private String       firstName;
    private String       lastName;
    @DBRef(db = "organizations")
    private Organization organization;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public Organization getOrganization() {
        return organization;
    }
    
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
    
    @Override
    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", organization="
               + organization + "]";
    }
    
}

Organization.java

@Document(collection = "organizations")
public class Organization {
    @Id
    private String id;
    private String name;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Organization [id=" + id + ", name=" + name + "]";
    }
    
}

The GET API request: http://localhost:8080/students?page=0&size=10&sort=id,desc&lastName=xyz

the repository method Page findAllByOrganizationId(String organizationId, Example example, Pageable pageable); should return all the records with matching organizationId and lastName=xyz, But it returns the records that match with the organization Id along with all the students irrespective of the lastName.

Is something wrong with the code?

Thank You!

1
Are you sure example matcher and findBy works together?Eklavya

1 Answers

0
votes

Finally found answer. findBy and Example Executor doesn't work together.

By setting the child object filter and query by Example Executor as shown below.

public Page<Student> getStudentsPage(Student student, Pageable pageable) {
        logger.debug("Getting Students : {}, {}", student, pageable);
        //gets organization Id of the student from the session
        String organizationId = userService.getUserMe().getOrganization().getId(); 
        student.setOrganization(new Organization(organizationId));
        Page<Student> studentPage = studentRepository.findAll(Example.of(student), pageable);
        
        logger.debug("Students: {}", studentPage.getContent());
        return studentPage;
    }    

This works as expected!!

Thanks!!