Reputation: 61
Given the following type hierachy:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
class A { }
@Entity
class B extends A {
String b;
}
@Entity
class C extends A {
String c;
}
@Entity
class D {
@ManyToOne
A a;
}
Now I would like to query all records of type D with 'a' of type C and field 'c' equals a given string parameter:
...
Root<D> root
CriteriaBuilder cb
cb.equal(root.get("a").get("c").as(String.class), "Test");
...
As foreseeable, I will get an exception telling me that attribute 'c' is unknown, as it is not defined at supertype A.
Any ideas, how to tell JPA Query that it should only join C for field 'a' at D AND add a condition to field 'c'?
Upvotes: 2
Views: 1059
Reputation: 696
CriteriaQuery<Tuple> q = criteriaBuilder.createTupleQuery();
Root<D> from = q.from(D.class);
Join<D, A> path = from.join(D_.a);
q.multiselect(path.get(A_.id)).where(criteriaBuilder.equal(path.type(), C.class));
Upvotes: 1