How can I get row with some status and latest id in spring repository?

1.3k views Asked by At

I have entity Item with fields - id,...,status

How can I write select for get row with last Id?

I tried this

Item findTopByStatusDesc(int status);

and

Item findOneByStatusDesc(int status);

Byt it not worked.

id name status
1   hz   0
2   hz2  1
3   hz3  1

I need get only one row where status = 1 and id latest 3 hz3 1

I need order DESC and get first row but I can not

1

There are 1 answers

0
farrellmr On

You can limit query results -

Item findTopByStatusOrderByStatusDesc(int status);

Or

Item findFirstByStatusOrderByStatusDesc(int status);

Ref -

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result