Bille Ibinabo
Bille Ibinabo

Reputation: 103

How can I order by date in jpa query

Please I want to write a jpa query for this table: payment_transaction
I am making a http request with a list of taxpayer references (since it is unique). I want to get all last payment_transaction record for all taxpayers with taxpayer_reference in my http request list of taxpayer_reference using a single query.
I know that:
paymentTransactionRepository.findAllByTaxPayerReferenceIn(List<String> taxPayerReferences)
will give me all the payment transaction records that has a taxPayerReference in the parameter I will pass but I want to get the last payment made by each taxPayerReference. I guess I should order by paymentDate in descending order and get the first record for each taxPayerReference as well
PS: In single jpa query
I don't know if that's possible. Please help me write the query.
Thanks enter image description here

Upvotes: 0

Views: 651

Answers (1)

Midhun Mohan
Midhun Mohan

Reputation: 689

You can try like below.

findByAgeOrderByLastnameDesc(Long age)

Which will be generated as below when you run

where x.age = ?1 order by x.lastname desc

For your particular example,

paymentTransactionRepository.findAllByTaxPayerReferenceInOrderByPaymentDateDesc(List<String> taxPayerReferences)

More examples on this available here

Upvotes: 2

Related Questions