How to add mulitple AND conditions to criteria in Spring Data
I found out what I was doing wrong. You don't need to use method 'andOperator' and another Criteria inside.
It would simply work by using 'and' method and then chain the following operator method you want to use.
Sharing the working solution to help other newcomers like me.
Criteria criteria = new Criteria();
criteria = criteria.and("siteCode").is(siteCode);
if (paymentMode != null) {
criteria = criteria.and("paymentMode").is(paymentMode);
}
if (planCode != null) {
criteria = criteria.and("packageCode").is(planCode);
}
if (status) {
criteria = criteria.and("expiryDateTime").gt(new Date());
} else {
criteria = criteria.and("expiryDateTime").lte(new Date());
}
Query query = new Query(criteria);