I have an entity where I have a field of type integer array.
class User {
@ElementCollection
@Column(name = "`location_ids`", columnDefinition = "int[]")
@Type(ListArrayType.class)
private List<Integer> locationIds;
@Column(name = "`email`")
private String email;
@Column(name = "`deletedAt`")
@Temporal(TIMESTAMP)
private LocalDateTime deletedAt;
}
I want to use criteria API to compare the requested locationIds list with the existing array. For example, I want to check if the location ID 1,2 or 3 is part of the "locationIds" array column or not.
final List<Predicate> predicates = new ArrayList<>();
predicates.add(criteriaBuilder.equal(root.get("email"), email));
predicates.add(criteriaBuilder.isNull(root.get("deletedAt")));
List<Predicate> arrayPredicates = new ArrayList<>();
final Expression<List<Integer>> exp = root.get("locationIds");
for (Integer id : locationIdsFromSearchModel) {
arrayPredicates.add(criteriaBuilder.isMember(id, exp));
}
predicates.add(criteriaBuilder.or(arrayPredicates.toArray(new Predicate[0])));
The error "operator does not exist: integer = integer[]" is thrown from the call criteriaBuilder.isMember(id, exp)
I looked up and it seems I need to do an "ANY" operation on the array. I didn't find any reference on how to do that with Criteria API.