Home > Software engineering >  Spring data jpa find by between on id field
Spring data jpa find by between on id field

Time:03-20

I am trying to retrieve some rows from my DB, like

select * from my_table where id between 1 and 100;

Is there any option in JpaRepository for between on primary key?

CodePudding user response:

Any custom method can be written in JPARepo. You need to make sure you are following JPA rules. The field name should exist in the method, In bellow method Id is my field name in my Entity class.

Entity Class

@Entity
@Setter@Getter
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String name;
}

Repo Class

@Repository
public interface CourseSprngDataRepo extends JpaRepository<Course, Long>{
    List<Course> findByIdBetween(Long l, Long m);
    List<Course> findByIdBetweenOrderByNameAsc(long l, long m);//Between and Order by another column ex
}
  • Related