I have a table ROOT and a table CHILDREN that contains rows related to ROOT.
I'd like to retrieve for each row of ROOT, an object that contains the ROOT object and the number of rows related to the ROOT.id in the CHILDREN table.
This can be achieved quite easily in SQL, but I am wondering how to write that in JPQL
In SQL:
SELECT 
    r.*, 
    (SELECT COUNT(c.i_id) 
     FROM 
        children c 
    WHERE 
        c.rootId = r.id)
FROM root r;
I tried to rewrite it in JPA but it keeps failing with the following error
Caused by: org.hibernate.HibernateException: Errors in named queries: xxx
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate 
           appropriate constructor on class RootExtended...
Java class:
package plop;
public class RootExtended{
    private Root root;
    private Long count;
    public RootExtended(final Root root, final Long count) {
        this.root= root;
        this.count= count;
    }
    // getters and setters to follow
}
JPQL:
SELECT 
    new plop.RootExtended(r, 
                    (SELECT 
                         count(c.id) 
                     FROM 
                         Child as c 
                     WHERE 
                         c.rootId = r.id
                     )
                    ) 
FROM 
    Root as r 
Any idea ?