2

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?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
lelmarir
  • 593
  • 2
  • 7
  • 24

1 Answers1

3

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.

logan
  • 3,416
  • 1
  • 33
  • 45
  • yes, the query just work also without the CC root but when i use getRoots() ther's no sign of CC while it's used. – lelmarir Feb 28 '11 at 07:17
  • What if the connection between two tables is not using foreign keys? What if the connection is using some string field? What if while fetching data from `CC`, we want to check certain condition with the field in `CC` and also that it is not used in `AA`? How can this be achieved? – Chetan Oswal Jun 29 '21 at 08:39