'Global Filter with JPQL Spring
I'm doing some projects a many them ask to do a global filter, my question is is there any chance to do a global filter without use to many if to check if the user add that filter or no? here an example of one of my projects: in repository I add this code:
@Query("from students s join Class_Room c on s.classRoom.id=c.id join teachers t on t.id=c.id " +
"where c.name= :className AND t.subject= :subject AND s.name= :name")
List<Student> studentClassProf(@Param("className") String className, @Param("subject") String subject, @Param("name") String name);
@Query("from students s join Class_Room c on s.classRoom.id=c.id join teachers t on t.id=c.id " +
"where c.name= :className AND t.subject= :subject")
List<Student> studentClassProf(@Param("className") String className, @Param("subject") String subject);
And in my controller class I using this:
@GetMapping("/globalSearch")
public ResponseEntity<String> globalSearch(String studentName, String teacherName, String className, String subject) throws MyNotFoundException {
if (studentName != null && teacherName != null && className != null && subject != null) {
studentRepo.studentClassProf(className, subject, studentName);
}
if (studentName == null && teacherName != null && className != null && subject != null) {
studentRepo.studentClassProf(className, subject);
}
....
So what can I do without add a lot of if to check if the field is null or not or I can't to do nothing to solve and I always have to write a lot of if block?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
