我有一个DAO,可以过滤列表。
public List<User> filterUser(UserView userView) {
List<User> userList;
Long officeId = userView.officeId;
String firstName = userView.firstName;
String secondName = userView.secondName;
String middleName = userView.middleName;
String position = userView.position;
String docCode = userView.docCode;
String citizenshipCode = userView.citizenshipCode;
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<User> userQuery = criteriaBuilder.createQuery(User.class);
Root<User> userRoot = userQuery.from(User.class);
Join<User, Document> docJoin = userRoot.join("document");
Join<Document, DocType> docTypeJoin = docJoin.join("docType");
Join<Document, Country> countryJoin = docJoin.join("country");
userQuery.select(userRoot);
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
if (firstName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("firstName"), firstName));
}
if (secondName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("secondName"), secondName));
}
if (middleName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("middleName"), middleName));
}
if (position != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("position"), position));
}
if (docCode != null) {
userQuery.where(criteriaBuilder.equal(docTypeJoin.get("code"), docCode));
}
if (citizenshipCode != null) {
userQuery.where(criteriaBuilder.equal(countryJoin.get("code"), citizenshipCode));
}
userList = em.createQuery(userQuery).getResultList();
return userList;
}
如果我请求字段officeId
,firstName
,secondName
等,则一切正常。
docCode
和citizenshipCode
的问题。例如,当我发送officeId
和docCode
时,过滤器仅适用于docCode
,而不适用于officeId
。
如果我在这样的地方更改行:
userQuery.where(criteriaBuilder.equal(docTypeJoin.get("code"), docCode));
及之后
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
我的列表中只有officeId
,而没有docCode
。