Hibernate 5,条件查询where子句有2个限制

Hibernate 5, Criteria Query for where clause with 2 restrictions

对于员工 Table

public class Employee {
    
    private Long id;
    
    private int deptId;
    
    private String name;
    
    private LocalDateTime joiningDate;
    
    private LocalDateTime seperatedDate;

}

我想建立一个符合以下两个条件的标准

criteriaQuery.where((EmployeeRoot.get("deptId").in(request.getDeptIdList())));
criteriaBuilder.greaterThanOrEqualTo(EmployeeRoot.get("joiningDate"), startDate)); 

但是只取最新的Predicate。我如何将两者结合起来,例如 [Where {condition1} + AND {condition2}] ?

你应该这样做:

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();