Reputation: 45
I have this code below:
public Optional<KoKur> getExchangeRateFromList(final Integer code, List<KotukOdeme> paymentList){
List<LocalDate> paymentDateList = null;
for(KotukOdeme kotukOdeme : paymentList){
paymentDateList.add(kotukOdeme.getOdemeTarihi());
}
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<KoKur> query = cb.createQuery(KoKur.class);
Root<KoKur> koKurRoot = query.from(KoKur.class);
query.where(
cb.and(
cb.equal(koKurRoot.get("dovizTip"), code)
)
);
TypedQuery<KoKur> typedQuery = entityManager.createQuery(query);
List<KoKur> koKurList = typedQuery.getResultList();
return koKurList.isEmpty() ? Optional.empty() : Optional.of(koKurList.get(0));
}
I am collecting list of localdates in paymentDateList, I want to add these as IN clause in my query. So beside "code", also i want to add IN next to my AND clause. When I add like below IDE gives me error;
cb.in(koKurRoot.get("tarih"), paymentDateList)
How can I do that ?
Upvotes: 2
Views: 517
Reputation: 3166
This should work. Add other conditions to predicates array if you need
List<Predicate> predicates = new ArrayList<>();
predicates.add(query.get("tarih").in(paymentDateList))
query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
return entityManager.createQuery(query).getResultList();
Upvotes: 0