For an Employee Table
public class Employee {
private Long id;
private int deptId;
private String name;
private LocalDateTime joiningDate;
private LocalDateTime seperatedDate;
}
I want build a criteria with both of the below condition
criteriaQuery.where((EmployeeRoot.get("deptId").in(request.getDeptIdList())));
criteriaBuilder.greaterThanOrEqualTo(EmployeeRoot.get("joiningDate"), startDate));
But it only takes the latest Predicate. How can i combine both like [Where {condition1} + AND {condition2}] ?
You should do something like this:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> criteria = builder.createQuery(Employee.class);
Root<Employee> root = criteria.from(Employee.class);
criteria
.select(...)
.where(
builder.and(
root.get("deptId").in(request.getDeptIdList()),
builder.greaterThanOrEqualTo(root.get("joiningDate"), startDate))
)
);
entityManager.createQuery(criteria).getResultList();