Hibernate Aggregate Functions(Associations and Joins)
This example tries to make understand about the aggregate function of Hibernate with the help of example.
In Hibernate HQL queries are capable of returning the results of the aggregate functions on properties. Collections can also be used with the aggregate functions with the select clause. Aggregate functions supports the following functions:
- min(...), max(...), sum(...), avg(...).
- count(*)
- count(distinct...), count(all..), count(...)
The distinct and all keywords used above are identical as in SQL.
A unidirectional one-to-many association on a foreign key is rarely required.
<?xml version="1.0" encoding="UTF-8"?>
|
A unidirectional one-to-many association on a foreign key is rarely required.
<?xml version='1.0' encoding='utf-8'?>
|
In this example first we create the session object with the help of the SessionFactory interface. Then we use the createQuery() method of the Session object which returns a Query object. Now we use the openSession() method of the SessionFactory interface simply to instantiate the Session object. And the we retrieve the data from the database store it in that Query object and iterate this object with the help of Iterator and finally displays the requested data on the console.
Here is the hibernate code:
package net.roseindia;
|
Output:
log4j:WARN No
appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly. Hibernate: select dealer1_.name as col_0_0_, product0_.name as col_1_0_, sum(product0_.price) as col_2_0_ from Product product0_ inner join Dealer dealer1_ on product0_.did=dealer1_.id group by product0_.name Dealer Name Product Name Price Agrawal Computer 23100.0 Ritu HardDisk 2500.0 Agrawal Keyboard 1500.0 Ritu Laptop 200.0 Mohan Mobile 15000.0 Mohan PenDrive 200.0 |