0

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"));
Pra_A
  • 7,134
  • 12
  • 89
  • 170

2 Answers2

3

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);
}
riorio
  • 5,015
  • 5
  • 29
  • 70
1

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

List<Student> findAllByStatusOrderByStudentNameAsc(String status);
Johna
  • 1,416
  • 2
  • 12
  • 24