Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate inner select in from clause

I'd like to know if it's possible to specify a select clause in a from clause something like

select count(*) as Y, this_.NAME as A, sel2.C
from TABLE1 this_, 
    (select count(*) as C from 
        (select this_.NAME, this_.SEX 
        from TABLE1 this_ group by this_.NAME, this_.SEX) sel1
    ) sel2
group by this_.NAME, sel2.C;

I need a query like this in order to have the count number as an extra column in the outer query. I can't find out how to specify a select statement in a from, neither with hql nor with criteria.

Thank you.

Luca

like image 555
Luca Avatar asked Apr 07 '11 13:04

Luca


People also ask

How use inner join in Hibernate query?

beginTransaction(); String select = "FROM Employee e INNER JOIN Team t ON e. Id_team=t. Id_team"; Query query = session. createQuery(select); List elist = query.

Is subquery supported in HQL?

Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed. Note that HQL subqueries can occur only in the select or where clauses. Note that subqueries can also utilize row value constructor syntax.

What is the difference between JPQL and HQL?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.

Can we use group by in HQL?

The HQL HAVING clause is used with GROUP BY clause.


1 Answers

According to Hibernate documentation HQL subqueries can occur only in the select or where clauses:

Hibernate Community Documentation, Chapter 16. HQL - 16.3. Subqueries

like image 69
sayYeah Avatar answered Oct 01 '22 16:10

sayYeah