如何发现隐式多重根
这是我的展示案例代码:
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();
其中 AA 是映射表,c 是 CC 类型项(其中 CC 是另一个映射表):
好吧,我不允许插入图像,所以: Tables Schema
c 是引用表 CC 的外键
因此,上面的代码将打印“RootCount: 1”(仅一个根),但生成的查询将是这样的:
select aa.id, aa.a, aa.b, cc.t, aa.d from AA aa, CC cc where aa.c=cc.id
所以...两个根,但 q.getRoots() 只报告我明确定义的那个。
怎样才能得到真正的根呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你只有一个根。也就是说,没有什么可以阻止您声明多个根并手动连接它们。
诀窍是,您并不真正需要 CC 根。您可以像使用根一样使用
Path
。或者,您可以使用aa.join
。You do only have one root. That said, there is nothing stopping you from declaring multiple roots, and manually joining them.
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 useaa.join
.