List<FilterParameter> params = filterPredicate.getParameters(); String firstParam = params.size() > 0 ? params.get(0).getPlaceholder() : null; switch (filterPredicate.getOperator()) {
/** * Given a collection of filter predicates and a Hibernate query, populates the named parameters in the * Hibernate query. * * @param query The HQL query * @param predicates The predicates to extract named parameter values from */ protected void supplyFilterQueryParameters(Query query, Collection<FilterPredicate> predicates) { for (FilterPredicate filterPredicate : predicates) { if (filterPredicate.getOperator().isParameterized()) { boolean shouldEscape = filterPredicate.isMatchingOperator(); filterPredicate.getParameters().forEach(param -> { query.setParameter(param.getName(), shouldEscape ? param.escapeMatching() : param.getValue()); }); } } }
@Test public void testHQLQueryVisitor() throws Exception { List<Path.PathElement> p1Path = Arrays.asList( new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name") ); FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar"); List<Path.PathElement> p2Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "name") ); FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah"); List<Path.PathElement> p3Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "genre") ); FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi"); OrFilterExpression or = new OrFilterExpression(p2, p3); AndFilterExpression and = new AndFilterExpression(or, p1); NotFilterExpression not = new NotFilterExpression(and); HQLFilterOperation filterOp = new HQLFilterOperation(); String query = filterOp.apply(not, false); String p1Params = p1.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p2Params = p2.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p3Params = p3.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String expected = "WHERE NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND authors.name IN (" + p1Params + ")))"; Assert.assertEquals(query, expected); }