hibernateejbejbql

Hibernate 3.2.1 and EJBQL query within query


I'm trying to make sure the following is supported with the EJBQL capabilities in hibernate 3.2.1, but my google-fu has failed me. All I found was that nested queries aren't supported in EJB 2.x

Can I put a query within a query like this:

select count(*) from 
    (select myField from myTable group by myField having sum(case.....) ....... )

Solution

  • EBJQL does not support subqueries in the from clause (you can use them in select and where), so this exact query cannot be performed. You could try rewriting the query, in your case the following should be equivalent:

    select count(distinct e.myField) from myEntity e group by e.myField having ...
    

    Alternatively, just use createNativeQuery() and use your original query without rewriting it in HQL. This of course breaks portability.

    HINT: if you're using Eclipse, get the Hibernate plugin from http://www.jboss.org/tools or the marketplace. It allows you to write and execute queries directly from the IDE (see query prototyping in the Hibernate tools documentation). Other IDEs could have similar capabilities also.