简体   繁体   English

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

[英]Hibernate 5, Criteria Query for where clause with 2 restrictions

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}]?我怎样才能像 [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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM