0

I want to fetch id from my table with count limit=1.

What I tried?

Optional<Long> findFirstIdByServiceSeeker_Id(Long serviceSeekerId); // But it returns Entity. I need id alone

I have following solution to fix it, but I want to know how to fetch id along with limit?

Alternate solution:

 Optional<User> findFirstIdByServiceSeeker_Id(Long serviceSeekerId); 
 Optional<User> userOptional =  userRepository.findFirstIdByServiceSeeker_Id(serviceSeekerId);
 Long userId =userOptional.get().getId();// it will fix
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

2 Answers2

0

in jpa u can make it as follow ,

Optional<CustomType> findFirstIdByServiceSeeker_Id(Long serviceSeekerId);




public interface CustomType {
    getId();
    }
0

Try writing custom Query like this! (Modify as per the DB/SQL Conventions)

@Query("SELECT serviceSeekerId from USERS u where u.serviceSeekerId= :serviceSeekerId")

Long findFirstIdByServiceSeekerId(String id);

Refer similar links

https://www.baeldung.com/spring-data-jpa-query

Creating a custom query with Spring DATA JPA?

Harsh
  • 812
  • 1
  • 10
  • 23
  • Yes. I tried exact same query. Problem is it return list of ids. Because serviceSeekerId is not unique. I want to limit the results to 1. – Ranjithkumar Jun 14 '22 at 14:47