Subquery in From claus in JPA2 Criteria
===

I have a data model where an account can have multiple users:

    class Account {
        Long id;
    }
    class User {
        Long id;
        @ManyToOne
        Account account;
    }

  
I'd like to do the following query which displays the number of users of each account:

    select Account.id, NumUsers.num from Account, 
           (select Account.id as account_id, count(User.id) as num 
            from User join Account on User.account_id=Account.id 
            group by Account.id) as NumUsers 
    where Account.id=NumUsers.account_id;


I know I can re-write this specific query as:

    select Account.id, count(User.id) from Account join 
           User on User.account_id=Account.id group by Account.id

but I plan to create more complicated queries for reports that require more than one group by. I read here: http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html about the correct approach to multiple group by.

How can I create my query using JPA2 Criteria API?

Thanks,
Yoav.