Add replaceCriteria method. Check string for search.
@@ -52,6 +52,7 @@ public class UserFilter implements Specification<User> {
* @return a {@link Predicate}, may be {@literal null}.
*/
private Predicate hasFieldsLike(Root<User> r, CriteriaBuilder cb, String reg) {
+ reg = replaceCriteria(reg);
return cb.or(
cb.like(r.get(RepoConstants.FIRST_NAME), reg),
cb.like(r.get(RepoConstants.LAST_NAME), reg),
@@ -59,4 +60,19 @@ public class UserFilter implements Specification<User> {
cb.like(r.get(RepoConstants.REGISTRATION_DATE).as(String.class), reg)
);
}
+
+ /**
+ * Returns a String criteria for search
+ *
+ * @param criteria must not be {@literal null}.
+ * @return String creteria may be {@literal null}.
+ */
+ private String replaceCriteria(String criteria) {
+ criteria = criteria.trim();
+ criteria = criteria.replace("_", "\\_");
+ criteria = criteria.replace("%", "\\%");
+ criteria = "%" + criteria + "%";
+ return criteria;
+
+ }
}
