<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg(&#8230;), sum(&#8230;), min(&#8230;), max(&#8230;) , count(*), count(&#8230;), count(distinct &#8230;), count(all&#8230;) ) calculated from property values of all objects satisfying other query criteria.</p>
<p><b> Queries:</b><br />
For <b>max()</b><br />
<b>&#8220;SELECT MAX(rollNumber) FROM Student student&#8221;</b><br />
For<b> min():</b><br />
<b>&#8220;SELECT MIN(rollNumber) FROM Student student&#8221; </b></p>
</div>
<p><b>HibernateTestDemo.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import java.util.Iterator; 
 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
 
public class HibernateTestDemo { 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 Session session = sessionFactory.openSession(); 
 session.beginTransaction(); 
 String SQL_QUERY = "SELECT MAX(rollNumber) FROM Student student"; 
 Query query = session.createQuery(SQL_QUERY); 
 
 for(Iterator it=query.iterate();it.hasNext();) 
 { 
 Long row = (Long) it.next(); 
 System.out.print("MAX ROLL NUMBER: " + row); 
 } 
 session.getTransaction().commit(); 
 session.close(); 
 } 
} 
</pre>
<div id="ads-id" align="center"></div>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).</div>
<div style="color: red;">log4j:WARN Please initialize the log4j system properly.</div>
<p>Hibernate: select MAX(student0_.ROLL_NUMBER) as col_0_0_ from STUDENT student0_<br />
MAX ROLL NUMBER: 8</p>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/05/aggregate.jpg" border="0" /></div>
<p> ;</p>
<h2 style="color: blue;">For min() function</h2>
<p><b>HibernateTestDemo.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import java.util.Iterator; 
 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
 
public class HibernateTestDemo { 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 Session session = sessionFactory.openSession(); 
 session.beginTransaction(); 
 String SQL_QUERY = "SELECT MIN(rollNumber) FROM Student student"; 
 Query query = session.createQuery(SQL_QUERY); 
 
 for(Iterator it=query.iterate();it.hasNext();) 
 { 
 Long row = (Long) it.next(); 
 System.out.print("MIN ROLL NUMBER: " + row); 
 } 
 session.getTransaction().commit(); 
 session.close(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).</div>
<div style="color: red;">log4j:WARN Please initialize the log4j system properly.</div>
<p>Hibernate: select MIN(student0_.ROLL_NUMBER) as col_0_0_ from STUDENT student0_<br />
MIN ROLL NUMBER: 1</p>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/05/aggregate1.jpg" border="0" /></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/hibernate-count-query/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/hibernate-batch-processing_10/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '686'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…