Surya Chandra
Surya Chandra

Reputation: 59

Spring data jpa find by between on id field

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?

Upvotes: 1

Views: 1140

Answers (1)

cvsr.sarma
cvsr.sarma

Reputation: 909

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
}

Upvotes: 3

Related Questions