Search code examples
hibernatejpapostgresql-9.1hibernate-criteriajpa-2.1

JPA CriteriaBuilder FOR Filtering from Postgres Array


Need help in creating filter Predicate for following query

SELECT * FROM table
    where
    'test_value' IN (SELECT unnest(data_array));

OR

Select *
FROM public.table
where 'test_value' = ANY (data_array);

JPA CriteriaBuilder FOR SELECTING from Postgres Array.

Table Format

id ,             
(char varying)

data_array
(text[])

I was trying to create filter for above query but finding it hard to get actual query filter working.

Expression function = builder.function("unnest", String.class, root.get(criteria.getKey()));

More info :-

Predicate has the code

class FilterSpecificaion implements Specification {
 public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {...} 
....
}

Solution

  • I found a work around with Postgres 9.5 we have more builtin functions. One of them is "array_positions" and I repurposed it to check if element is present. https://www.postgresql.org/docs/9.5/functions-array.html