I have an entity Document and enum Label (not my real case, I am using analogy). The Document can have set of Labels. The mapping of labels is following:
@Entity
public class Document {
...
@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@Column(name = "labels")
private Set<Label> labels = new HashSet<>();
...
}
It means labels are mapped into separated table with two columns (document_id, value) but in Java it is just enum
I need to select Documents that DO NOT have any of listed labels. In SQL it looks like this:
select D.id
from document D left join label L
on D.id = L.document_id and L.value in('label1','label2',...)
where L.document_id is null
But I don't know how to write it in JPA Criteria API. I don't know how to express the foreign key in labels table. The JPA predicate should be something like this
CriteriaBuilder cd = ...
SetJoin<Object, Object> labelsJoin = root.joinSet("labels", JoinType.LEFT);
cb.and(labelsJoin .in("label1","label2"), cb.isNull(...???...)));
Here is my related SQL question
Thanks in advance for your suggestions. Lukas