Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a SELECT query in Hibernate includes Subquery COUNT(*)

Let's say we have a Category - Items one-to-many relation. I would like to do this

SELECT c.*, 
   (SELECT COUNT(*) FROM items i WHERE i.catId=c.id)
    AS itemCount
FROM category c

And let's say we have a Hibernate POJO "class Category".

My first question is I really couldn't figure out that from that query I get a List<Category> object right? And how can I access the "itemCount"? Because there's no Category.getItemCount()

And secondly, how can I write the Criteria query?

Thanks

like image 708
Seregwethrin Avatar asked Mar 24 '12 16:03

Seregwethrin


People also ask

How do I create a subquery in hibernate?

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?

Some of the commonly supported clauses in HQL are: 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.

How do you add subquery criteria?

In the first step, I instantiate a CriteriaQuery which returns Author entities. Then I call the subquery method on the CriteriaQuery to create a subquery that counts the Books written by the Author which is selected by the outer query. As you can see, I define the subquery in the same way as I create a CriteriaQuery.

What is the description for count () in hibernate?

What is the description for count () in Hibernate? The count() can be used to count any kind of values, including the number of rows in the query result. avg(): calculates the average of given numeric arguments. count() including count(distinct), count(all), and count(*): count the number of values/rows.


2 Answers

Seems like this is the answer I was looking for (into to POJO):

@Formula(value="(SELECT COUNT(*) FROM Items i WHERE i.id = id)")
@Basic(fetch=FetchType.EAGER)
public Integer getItemCount() {
   return this.taskCount;
}
like image 87
Seregwethrin Avatar answered Oct 05 '22 19:10

Seregwethrin


Depending on your circumstances and ability to create a view. I would just create a view out of your query:

CREATE VIEW CategoryItemsView AS 
    SELECT c.*,  
   (SELECT COUNT(*) FROM items i WHERE i.catId=c.id) 
    AS itemCount 
FROM category c 

afterwards you can query how ever you like...

SELECT * FROM CategoryItemsView WHERE ItemCount = 5

Additionally, you could use a GROUP BY to achieve a similar result but that depends on your columns and the schema of your tables.

So, something like this:

SELECT c.COLUMN1, c.COLUMN2, COUNT(*) AS ItemCount
FROM category c inner join items i on i.catID = c.Id
GROUP BY c.COLUMN1, c.COLUMN2
HAVING COUNT(*) = 2
like image 23
Bob Delavan Avatar answered Oct 05 '22 17:10

Bob Delavan