我尝试以下操作,但未应用LIKE过滤器,即它返回完整的结果集。有什么想法吗?正如您在下面的日志中看到的那样,LIKE标准就在那里,但我猜这些参数没有填充。
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(typeParameterClass);
// from
Root<T> from = criteriaQuery.from(typeParameterClass);
criteriaQuery.select(from);
// like
if (searchQuery != null && searchQuery.trim().length() > 0) {
List<Predicate> predicates = new ArrayList<Predicate>();
for (String name : getColumnNames(typeParameterClass)) {
Predicate condition = criteriaBuilder.like(from.<String>get(name), "%" + searchQuery + "%");
predicates.add(condition);
}
criteriaQuery.where(criteriaBuilder.or(predicates.toArray(new Predicate[] {})));
}
List<T> results = (List<T>) entityManager.createQuery(criteriaQuery).setFirstResult(firstResult)
.setMaxResults(maxResults).getResultList();
return results;
日志:
select
employee0_.id as id1_1_,
employee0_.AVATAR as AVATAR2_1_,
employee0_.AVERAGE_RATING as AVERAGE_3_1_,
employee0_.DISTANCE as DISTANCE4_1_,
employee0_.DRIVERS_LICENCE as DRIVERS_5_1_,
employee0_.EMAIL_ADDRESS as EMAIL_AD6_1_,
employee0_.FIRST_NAME as FIRST_NA7_1_,
employee0_.JOB_DESCRIPTION as JOB_DESC8_1_,
employee0_.JOB_DETAILS as JOB_DETA9_1_,
employee0_.JOB_TITLE as JOB_TIT10_1_,
employee0_.JOINING_DATE as JOINING11_1_,
employee0_.LASTACCESS_DATE as LASTACC12_1_,
employee0_.LAST_NAME as LAST_NA13_1_,
employee0_.MOBILE_NUMBER as MOBILE_14_1_,
employee0_.PASSWORD as PASSWOR15_1_,
employee0_.PHONE_NUMBER as PHONE_N16_1_,
employee0_.SALARY as SALARY17_1_,
employee0_.SALARY_TIMEFRAME as SALARY_18_1_,
employee0_.SEX as SEX19_1_,
employee0_.USERNAME as USERNAM20_1_,
employee0_.WORK_LAT1 as WORK_LA21_1_,
employee0_.WORK_LAT2 as WORK_LA22_1_,
employee0_.WORK_LAT3 as WORK_LA23_1_,
employee0_.WORK_LAT4 as WORK_LA24_1_,
employee0_.WORK_LAT5 as WORK_LA25_1_,
employee0_.WORK_LONG1 as WORK_LO26_1_,
employee0_.WORK_LONG2 as WORK_LO27_1_,
employee0_.WORK_LONG3 as WORK_LO28_1_,
employee0_.WORK_LONG4 as WORK_LO29_1_,
employee0_.WORK_LONG5 as WORK_LO30_1_
11:36:53,974 INFO [stdout] (default task-2) from
employee employee0_
11:36:53,974 INFO [stdout] (default task-2) where
employee0_.FIRST_NAME like ?
or employee0_.LAST_NAME like ?
or employee0_.PASSWORD like ?
or employee0_.EMAIL_ADDRESS like ?
or employee0_.PHONE_NUMBER like ?
or employee0_.MOBILE_NUMBER like ?
or employee0_.JOB_TITLE like ?
or employee0_.JOB_DESCRIPTION like ?
or employee0_.USERNAME like ?
or employee0_.JOB_DETAILS like ?
答案 0 :(得分:0)
试试这个:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(typeParameterClass);
// from
Root<T> from = criteriaQuery.from(typeParameterClass);
criteriaQuery.select(from);
// like
if (searchQuery != null && searchQuery.trim().length() > 0) {
List<Predicate> predicates = new ArrayList<Predicate>();
for (String name : getColumnNames(typeParameterClass)) {
//comparing same case name
Predicate condition = criteriaBuilder.like(criteriaBuilder.upper(from.<String>get(name)), "%" + searchQuery.toUpperCase() + "%");
predicates.add(condition);
}
//criteriaQuery.where(criteriaBuilder.or(predicates.toArray(new Predicate[] {})));
Predicate[] predicatesArr = new Predicate[predicates.size()];
predicates.toArray(predicatesArr);
query.where(predicatesArr);
}
List<T> results = (List<T>) entityManager.createQuery(criteriaQuery).setFirstResult(firstResult)
.setMaxResults(maxResults).getResultList();
return results;
建议在搜索时始终使用相同的案例。