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
classes/1/students/search/findByName?name=Tom
It gives error right now.
Advertisement
Answer
in StudentController
@GetMapping("/classes/{classId}/students/search/findByName") 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)
@Repository public interface CustomUserRepo extends MongoRepository<Student, String> { List<Student> findByNameAndClassId(String name, String classId); }