Java hibernate 分离条件、计数/拥有、查询
有人可以帮我查询吗?
事情是这样的:
我有两个表
1- 用户组
2- 用户
一组有很多用户,但问题是,表组保存了表用户上的用户数量。但有时这个数字是无效的,我想找到表组中的数字小于表用户中的用户的情况。
SQL查询将是这样的:
select
id_group,
count(user)
from
user inner join user
having
count(user) < group.number_of_users
In hibernate I can't do that,到目前为止我已经进入这个
DetachedCriteria dc = DetachedCriteria.forClass(Group.class);
dc.createAlias("userCollection", "uc");
dc.setProjection(Projections.count("uc.idUser"));
dc.add(Restrictions.lt("????????", "??????????");
提前感谢
can someone help me out with a query ?
Here´s the deal:
I have two tables
1- Group of users
2- Users
One group has a lot of users, but the thing is, the table groups holds the number of users it has on table users. But it happens that sometimes this number is invalid, I want to find the casees where the number in the table group is less then the users in the table users.
The SQL query would be like that:
select
id_group,
count(user)
from
user inner join user
having
count(user) < group.number_of_users
In hibernate I cant do that, so far I got into this
DetachedCriteria dc = DetachedCriteria.forClass(Group.class);
dc.createAlias("userCollection", "uc");
dc.setProjection(Projections.count("uc.idUser"));
dc.add(Restrictions.lt("????????", "??????????");
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不做一个
DetachedCriteria
来计算呢?然后你
dc.add(Restrictions.lt(detachedCriteria, "?????????");
Why don't you do a
DetachedCriteria
that is the count?Then you
dc.add(Restrictions.lt(detachedCriteria, "??????????");