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

2 Answers 2

0

in jpa u can make it as follow ,

Optional<CustomType> findFirstIdByServiceSeeker_Id(Long serviceSeekerId);




public interface CustomType {
    getId();
    }
Sign up to request clarification or add additional context in comments.

Comments

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?

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.