0
我嘗試以下操作,但LIKE篩選器未應用,即返回完整的結果集。請有任何想法嗎?正如你可以在下面的日誌中看到的那樣,LIKE標準在那裏,但我猜這些參數沒有被填充。JPA 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 ?
它返回什麼,你期望什麼以及你用哪個關鍵字搜索? – Apostolos
謝謝。出於某種原因,我的服務器沒有應用新的代碼,然後我得到了錯誤。我的代碼實際上工作。我很欣賞你的努力 – Richard