0

I have this Spring JPA repository:

@Repository
public interface MerchantUserRepository extends JpaRepository<Users, Integer> {

    Optional<Users> findByIdAndTypeIn(Integer id, String... types);
}

Is there some way to limit the returned number of rows to 1?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

0

You add a Pageable param to your query:

Optional<Users> findByIdAndTypeIn(Integer id, String... types, Pageable page);

For one result you create it like:

Pageable firstRow = PageRequest.of(0, 1);

You may consider adding a Sort object also for your query in that case

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63