I am using Spring Specifications in my project. The sample Entity class is shown below.
public class TestEntity {
@Id
private String fieldOne;
private List<String> fieldTwo; // This is implemented as Postgres Array data type
}
For Searching purposes, I am using the below POJO class, where the operation EQUALS and IN are supported.
public class SearchCriteria <T> {
private String key;
private String operation;
private T value; // Used for the EQUALS operation
private List<T> values; // Used for the IN operation
}
I have no problem in implementing the EQUALS and IN operations for the fields fieldOne as shown below:
switch (OperationType.valueOf(searchCriteria.getOperation())) {
case EQUALS:
return
criteriaBuilder.equals(root.get(searchCriteria.getKey()),
searchCriteria.getValue());
CASE IN:
return
criteriaBuilder.in(root.get(searchCriteria.getKey()))
.value(searchCriteria.getValues());
}
Now, in case of fieldTwo, I am able to implement the EQUALS operation. EQUALS essentially means that one of the fieldTwo values matches the T value in the Search Criteria. The LIKE function is able to meet my requirements in this case, as shown below:
switch (OperationType.valueOf(searchCriteria.getOperation())) {
case EQUALS:
return criteriaBuilder.like(
criteriaBuilder.lower(root.get(searchCriteria.getKey()).as(String.class)),
"%" + searchCriteria.getValue() + "%");
}
However, I am having trouble implementing the IN operation for fieldTwo. IN essentially means that at least one of the entries in fieldTwo matches one of the entries in the List values in the Search Criteria. Any ideas on how to implement this?
NOTE: I know that if I denormalize the fieldTwo values into a separate table, solving this issue is trivial. However, for my use case, I want to use the Postgres array data type in my entity.