I am stuck with a problem concerning JPA-2.0 queries with relationships. How would it be possible to select any Dataset
with at least one Event
with type = B
?
@Entity
class Dataset {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "dataset")
public List<Event> events;
}
@Entity
class Event {
@ManyToOne
@JoinColumn
public Dataset dataset;
public Type type;
}
enum Type {
A, B, C
}
My starting point is
CriteriaBuilder _builder = em.getCriteriaBuilder();
CriteriaQuery<Dataset> _criteria = _builder.createQuery(Dataset.class);
// select from
Root<Dataset> _root = _criteria.from(Dataset.class);
_criteria.select(_root);
// apply some filter as where-clause (visitor)
getFilter().apply(
_root, _criteria, _builder, em.getMetamodel()
);
// how to add a clause as defined before?
...
Any ideas on this. I tried to create a subqueries as well as a join, but I somehow did it wrong and always got all datasets as result.