开发者

How to discover Implicit multiple root

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 mappe开发者_JAVA百科d 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜