0

I have a situation where I need to extract purchase records based on User's Cards (number of cards would vary from user to user) using JPA.

The equivalent SQL query would be something like: select * FROM Purchase WHERE cardId=10 OR cardId=1 OR.... ORDER BY purchasedate desc;

My DAO method signature is:

public List<Purchase> getPurchases(List<Card> cardsList)

I believe that I will have to use criteria query but I'm not quite sure how to implement it.

Abhay
  • 167
  • 3
  • 16
  • 1
    Take a look at this post : [POST](https://stackoverflow.com/questions/4378824/adding-in-clause-list-to-a-jpa-query) – k.hammouch Sep 23 '20 at 10:17

1 Answers1

1

I suggest you to use IN statement instead of multiple ORs

select * FROM Purchase WHERE cardId in(1, 10) ORDER BY purchasedate desc

So you can use

public List<Purchase> getAllByCardIdIn(List<Long> cardIds)

Oleksii Valuiskyi
  • 2,691
  • 1
  • 8
  • 22