Hibernate从多个表中计数 [英] Hibernate count from multi tables

查看:136
本文介绍了Hibernate从多个表中计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有4个表:

我想写一个HQL查询,它返回来自不同列的4个表的特定计数。 Tab1 Tab2 Tab3 Tab4 ,我想得到这样的东西:

  | Tab1 | Tab2 | Tab3 | Tab4 | 
| .1 .... | ..13 ... | ... 7 ... | .... 0 ... |

Tab#中的所有记录都有它们的记录自己的 id ObjectID ObjectFetch



我试过这样的事情:

  select DISTINCT 
(select计数(*)从Tab1其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab2中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
(select count *)from Tab3其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab4中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
来自Tab1,Tab2,Tab3 ,Tab4

但是它根本不起作用,为什么呢?

解决方案

我找到了一个解决方案,根本就不需要在 FROM 之后使用所有表格,我们可以只使用1,它只是一个正确的语法,我们也可以使用一个virtualTable,所以正确的HQL

 选择DISTINCT 
(从Object Tab中的Tab1中选择count(*) (从Tab2中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab3中选择count(*),其中ObjectFetch = :fetch和ObjectID =:id),
(从Tab4中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id)
from Tab1

及其全部OK :) thx。


I would like to write a single HQL query that return a specific count from 4 tables in different columns.

I have 4 tables: Tab1, Tab2, Tab3 and Tab4, and I would like to get something like this:

|Tab1   | Tab2  | Tab3  | Tab4   |
|..1....|..13...|...7...|....0...|

All records in Tab#s have their own id and ObjectID and ObjectFetchcolumns

I have tried something like this:

select DISTINCT
(select count(*) from Tab1 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab2 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab3 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab4 where ObjectFetch=:fetch and ObjectID=:id),
from Tab1, Tab2, Tab3, Tab4

But it doesn't work at all, any idea why?

解决方案

I found a solution, simply there is no need to use all tables after FROM we can use only 1, it is just for a correct syntax, also we can use a virtualTable, so the right HQL

select DISTINCT
(select count(*) from Tab1 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab2 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab3 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab4 where ObjectFetch=:fetch and ObjectID=:id)
from Tab1

and its all ok :) thx.

这篇关于Hibernate从多个表中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆