Like SQL, hibernate also supports aggregate functions. For example, if I want to get the average price of the products in each category, or how many products are there in each category, we can use the functions like count, min max, sum and average. So to demonstrate the same, let's comment out this function call. And then I'm going to type here, get category voice, product count. We don't have this function, if you press Command one, or Ctrl one on Windows, you get this new function. I press tab, change this to package, delete the two, my SQL would be something like this, where I want the category dot category name.
And then I type your count of P from product p group by P dot category, dot category name Of course, we have to type here P dot, because the moment you use an alias for the product type, you have to use the alias to access each of those members. Now the query is ready. Once again, we are getting an array of a string and a number. So we have to follow the same method, we can actually copy the entire code from here, paste over here, save this run the same, and we should see the category wise product count listed out. So if you want to add even additional functions, for example, if I want to say here, average of P dot unit price, I should be able to get that as well. And we can print the same here by typing average equals to plus data I have to save it and run.
We should now see the average price along with the count