0
votes

I am developing Spring Boot + Spring Data JPA + Postgres + Lombok example. In this example, I want to fetch all student firstName is ASC order and whose status in Active.

I developed below query, which works fine, but I dont see a way to also use status=Active here in JpaRepository query.

NOTE: In my case, status field is Enum in Java.

Is there any way if we can do the same ? I know I can fetch all students and then using streams can easily filter, but using JpaRepository query, is there any way?

List<Student> students = studentRepository.findAll(new Sort(Sort.Direction.ASC, "studentName"));
2
you can do this findAllByStatusOrderByStudentNameAsc - Med Elgarnaoui

2 Answers

3
votes

In your StudentRepository interface, that extends Repository / JpaRepository you can add a method signature like that:

public interface StudentRepository extends ....{
  List<Student> findAllByStatusOrderByStudentNameAsc(String status);
}
1
votes

Just put the following method signature in your repository and call it where you need with argument 'Active'.

List<Student> findAllByStatusOrderByStudentNameAsc(String status);