Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can HQL Select on the result set of another query?

Tags:

Can HQL Select on the result set of another query? For example:

SELECT COUNT(*) FROM (SELECT * FROM Table) 

I can do it in SQL but when I tried like above in HQL, it just showed me syntax error "unexpected token: ( near line 1, column 22 ..."

like image 454
Red Bit Avatar asked Apr 03 '13 08:04

Red Bit


People also ask

Is subquery supported in HQL?

Subqueries. For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.

Can we use select * in HQL?

HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee . We can also create alias such as from Employee emp or from Employee as emp . HQL Join : HQL supports inner join, left outer join, right outer join and full join.

Which method is used to get a result from HQL query?

The object of Query can be obtained by calling the createQuery() method Session interface.

Which is faster SQL or HQL?

You can select only certain columns with HQL, too, for example you can use select column from table in HQL. Native SQL is not necessarily faster than HQL. HQL finally also is translated into SQL (you can see the generated statement when running the application with the show_sql property set to true).


2 Answers

HQL does support subqueries, however they can only occur in the select or the where clause. The example you provide would best be wrote as a straight statement in HQL. For example:

select count(*) from table t  (where table is the entity name) 

If the query involves a more complicated statement than (select * from Table), I would recommend putting this logic into a view and then creating an entity based off of this view.

For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.

Example

from DomesticCat as cat where cat.name not in (     select name.nickName from Name as name ) 
like image 146
Kevin Bowersox Avatar answered Oct 20 '22 01:10

Kevin Bowersox


there is no way to do subquery in from clause in HQL even if the database support it, I solved this problem by putting the query into the sql as a store procedure, then call the procedure in HQL. For example:

Insert the procedure into your sql:

DELIMITER $$ CREATE PROCEDURE `procedure_name`(   `arg_name` INT, ) BEGIN      your query here END; $$ DELIMITER ; 

Then, if you use hibernate, call this procedure from java code as below:

Query query = session.createSQLQuery("CALL procedure_name(:arg_name)"); query.setParameter("arg_name", args); List list = query.list(); 

Hope this can help you.

like image 41
shinxg Avatar answered Oct 20 '22 00:10

shinxg