In this section, we will show you, how to use the Max() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg(...), sum(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all...) ) calculated from property values of all objects satisfying other query criteria.
Following is a aggregate function (max() function) with their respective syntax.
max( [ distinct | all ] object.property)
The Max() function aggregates the maximum value of the given column.
Table Name: insurance
| ID | insurance_name | invested_amount | investement_date |
| 2 | Life Insurance | 25000 | 0000-00-00 00:00:00 |
| 1 | Givan Dhara | 20000 | 2007-07-30 17:29:05 |
| 3 | Life Insurance | 500 | 2005-10-15 00:00:00 |
| 4 | Car Insurance | 2500 | 2005-01-01 00:00:00 |
| 5 | Dental Insurance | 500 | 2004-01-01 00:00:00 |
| 6 | Life Insurance | 900 | 2003-01-01 00:00:00 |
| 7 | Travel Insurance | 2000 | 2005-02-02 00:00:00 |
Here is the java code to retrieve the maximum value of "invested_amount" column from insurance table:
package roseindia.tutorial.hibernate;
|
Output:
| log4j:WARN No appenders
could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly. Hibernate: select max(insurance0_.invested_amount) as col_0_0_ from insurance insurance0_ Max Invested Amount: 25000 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Hibernate Max() Function (Aggregate Functions) View All Comments
Post your Comment