HQLUtils { public static void appendBlacklistCriteria(final StringBuilder toAppendTo, final List<Object> params, final String selector, final List<Pair<String, List>> blacklist) { if (blacklist != null) { final String join = " and "; boolean hasOneCriteria = false; for (final Pair<String, List> props : blacklist) { final List prop = props.getSecond(); if (CollectionUtils.isNotEmpty(prop)) { for (final Object val : prop) { String matchProp; String matchMode = " not like "; Object matchValue; if (val instanceof String) { if (StringUtils.isBlank((String) val)) { continue; } matchProp = " lower(" + selector + "." + props.getFirst() + ") "; matchValue = HQLUtils.criteriaIlikeAnywhere((String) val); } else { matchProp = " " + selector + "." + props.getFirst() + " "; matchValue = val; } final int pIdx = params.size() + 1; if (hasOneCriteria) { toAppendTo.append(join); } else { if (pIdx == 1) { toAppendTo.append(" where (\n"); } else { toAppendTo.append(" and (\n"); } } toAppendTo.append(" (" + matchProp + matchMode + "?" + pIdx + ") \n"); params.add(matchValue); hasOneCriteria = true; } } } if (hasOneCriteria) { toAppendTo.append(")"); } } } private  HQLUtils(); static String criteriaIlikeAnywhere(final String value); static String criteriaLikeAnywhere(final String value); static String criteriaIeq(final String value); static String criteriaEq(final String value); static Object criteriaInTest(final Collection value); static Collection criteriaIn(final Collection value); static void appendBlacklistCriteria(final StringBuilder toAppendTo,
                                               final List<Object> params,
                                               final String selector,
                                               final List<Pair<String, List>> blacklist); static void appendFilterCriteria(final StringBuilder toAppendTo,
                                            final List<Object> params,
                                            final String selector,
                                            final Map<String, List> filter);  }
@Test public void testAppendBlacklistCriteria() throws Exception { final List<Pair<String, List>> blacklist = new ArrayList<>(); assertWithBlacklist(null, null, "select from XEntity e "); blacklist.add(new Pair<>("prop1", Collections.singletonList("Val1"))); blacklist.add(new Pair<>("prop2", null)); blacklist.add(new Pair<>("prop3", Collections.singletonList(""))); assertWithBlacklist(blacklist, null, "select from XEntity e where (\n ( lower(e.prop1) not like ?1) \n)", "%val1%"); blacklist.clear(); blacklist.add(new Pair<>("prop1", Arrays.asList("Val1", "Val2"))); assertWithBlacklist(blacklist, null, "select from XEntity e where (\n ( lower(e.prop1) not like ?1) \n and ( lower(e.prop1) not like ?2) \n)", "%val1%", "%val2%"); final Map<String, List> filter = new LinkedHashMap<>(); assertWithFilter(null, "select from XEntity e "); filter.put("prop1", Collections.singletonList("Val1")); filter.put("prop2", null); filter.put("prop3", Collections.singletonList("")); assertWithBlacklist(blacklist, filter, "select from XEntity e where (\n ( lower(e.prop1) like ?1) \n) and (\n ( lower(e.prop1) not like ?2) \n and ( lower(e.prop1) not like ?3) \n)", "%val1%", "%val1%", "%val2%"); }