Skip to content

How to search in parent entity scope when using spring boot data rest

Now I know how to findBySomething on root level entity.

Let’s say I have a one to many relation of class -> student

I can now students/search/findByName?name=Tom

But it will give me Tom in all classes. Can I do something like


It gives error right now.



in StudentController

public ResponseEntity<?> findStudent(@PathParam("name") String name,
                    @PathVariable("classId") String classId) {
    return ResponseEntity.ok(studentService.findByNameAndClassId(name, classId));

in StudentService

public List<Student> findByNameAndClassId(String name, String classId) {
    return studentRepository.findByNameAndClassId(name, classId);

in StudentRepository (I use mongoDB; if u use SQL , extends JpaRepository)

public interface CustomUserRepo extends MongoRepository<Student, String> {
    List<Student> findByNameAndClassId(String name, String classId);
User contributions licensed under: CC BY-SA
3 People found this is helpful