How to discover Implicit multiple root

4k views Asked by At

here's my show case code:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<AA> aa = q.from(AA.class);

q.multiselect(aa.get("id").alias("id"),
articolo.get("a").alias("a"),
articolo.get("b").alias("b"),
articolo.get("c").get("t").alias("c"),
articolo.get("d").alias("d"));

System.out.println("RootCount: "+q.getRoots().size());

Query query = em.createQuery(q);
List<Tuple> list = query.getResultList();

Where AA is a mapped Table and c is a CC type item (in wich CC is another mapped table):

well, i'm not allowed to insert an image, so: Tables Schema

c is a foreignkey referencing the table CC

So, the above code will print "RootCount: 1" (only one root) but the resulting query would be something like this:

select aa.id, aa.a, aa.b, cc.t, aa.d from AA aa, CC cc where aa.c=cc.id

so...two roots, but the q.getRoots() report only the one i explicitly have defined.

How can i get the real roots?

1

There are 1 answers

2
logan On BEST ANSWER

You do only have one root. That said, there is nothing stopping you from declaring multiple roots, and manually joining them.

Root<AA> aa = query.from(AA.class)
Path<CC> aaToCc = aa.get("c");
Root<CC> cc = query.from(CC.class)

cb.where(cb.equal(aaToCcId, cc));

The trick is, you don't really need the CC root. You can just use teh Path<CC> pretty much like a root. Alternatively, you can use aa.join.