I am using:
- Hibernate 4.3.5
- Spring JPA 1.6.0
- Javax Persistence API 2.1
The "refcodemailing" column is defined as an array of int: int[]
My entity object:
@Entity
@Table
public class CalendarEvent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id = 0;
@Convert(converter = IntegerArrayConverter.class)
@Column(name = "refcodemailing")
private final List<Integer> mailingCodes = new ArrayList<>();
// ....
}
I am trying to filter the column array with the following JPA Specification method:
private final List<MailingCode> mailingCodes = new ArrayList<>();
@Override
public Predicate toPredicate(Root<CalendarEvent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// Mailing codes
if(!mailingCodes.isEmpty()){
List<Predicate> mailingCodePred = new ArrayList<>();
for(MailingCode mailingCode: mailingCodes){
restrictions.add(cb.isMember(mailingCode.getId(), root.<List<Integer>>get("mailingCodes")));
}
restrictions.add(cb.and(cb.isNotNull(root.<List<Integer>>get("mailingCodes")), cb.or(mailingCodePred.toArray(new Predicate[]{}))));
}
}
But the following exception is thrown:
java.lang.IllegalArgumentException: unknown collection expression type [org.hibernate.jpa.criteria.path.SingularAttributePath]
at org.hibernate.jpa.criteria.CriteriaBuilderImpl.isMember(CriteriaBuilderImpl.java:1332)
at com.cos.center.agenda.CalendarEventQuery.toPredicate(CalendarEventQuery.java:100)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:521)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:472)
Is there a way to do it?