简体   繁体   中英

Java hibernate Detached criteria, count / having, query

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

Why don't you do a DetachedCriteria that is the count?

Then you dc.add(Restrictions.lt(detachedCriteria, "??????????");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM