Hibernate max() Function
In this tutorial you will learn about how to use HQL max() function.
max() function in HQL returns the largest value from the selected properties. This function can be simply written with select clause as :
select max(gd.numOfGoodsOrder) from GoodsDealer gd
Hibernate allows the different keywords as the SQL to be written with the max() function.
for example : max( [ distinct | all ] object.property )
Example :
An example is being given below will demonstrate you how to embed the HQL max() function within your code. In this example at first I have created a table named goodsdealer ( goodsDealerId(int, pk), orderedGoodsName(varchar), numOfGoodsOrder(int), priceOfGoods(int) ) then I have created a POJO / Persistent class named GoodsDealer.java that contains some properties and created a mapping xml file named GoodsDealer.hbm.xml ( followed the convention) where mapped the table and class with their respective fields and properties. Then created a configuration file hibernate.cgf.xml that will provide information to the hibernate using which hibernate will create connection pool and the required environment setup. Finally created a main class where I shall use the max() function with the select clause.
Complete Code
goodsdealer table
CREATE TABLE `goodsdealer` ( `goodsDealerId` int(15) NOT NULL, `orderedGoodsName` varchar(15) default NULL, `numOfGoodsOrder` int(20) default NULL, `priceOfGoods` int(10) default NULL )
GoodsDealer.java
package roseindia; public class GoodsDealer { private int goodsDealerId; private int numOfGoodsOrder; private int totalPrice; private int priceOfGoods; private String orderedGoodsName; public GoodsDealer() { super(); } public GoodsDealer(int totalPrice, int priceOfGoods, int goodsDealerId, int numOfGoodsOrder, String orderedGoodsName) { this.totalPrice = totalPrice; this.priceOfGoods = priceOfGoods; this.goodsDealerId = goodsDealerId; this.numOfGoodsOrder = numOfGoodsOrder; this.orderedGoodsName = orderedGoodsName; } public int getTotalPrice() { return totalPrice; } public void setTotalPrice(int totalPrice) { this.totalPrice = totalPrice; } public int getPriceOfGoods() { return priceOfGoods; } public void setPriceOfGoods(int priceOfGoods) { this.priceOfGoods = priceOfGoods; } public int getGoodsDealerId() { return goodsDealerId; } public void setGoodsDealerId(int goodsDealerId) { this.goodsDealerId = goodsDealerId; } public int getNumOfGoodsOrder() { return numOfGoodsOrder; } public void setNumOfGoodsOrder(int numOfGoodsOrder) { this.numOfGoodsOrder = numOfGoodsOrder; } public String getOrderedGoodsName() { return orderedGoodsName; } public void setOrderedGoodsName(String orderedGoodsName) { this.orderedGoodsName = orderedGoodsName; } }
GoodsDealer.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="roseindia"> <class name= "GoodsDealer" table="goodsdealer"> <id name= "goodsDealerId" type="int" column="goodsDealerId"> <!-- <generator class="increement"/>--> </id> <property name="numOfGoodsOrder"> <column name="numOfGoodsOrder"/> </property> <property name="orderedGoodsName"> <column name="orderedGoodsName"/> </property> <property name="totalPrice"> <column name="totalPrice"/> </property> <property name="priceOfGoods"> <column name="priceOfGoods"/> </property> </class> </hibernate-mapping>
HibernateMaxFunction.java
package roseindia; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateMaxFunction { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; public static void main(String args[]) { Session session = null; try { try { Configuration cfg= new Configuration().addResource("roseindia/GoodsDealer.hbm.xml"); cfg.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); sessionFactory = cfg.buildSessionFactory(serviceRegistry); } catch(Throwable th) { System.err.println("Failed to create sessionFactory object." + th); throw new ExceptionInInitializerError(th); } session = sessionFactory.openSession(); Query query = session.createQuery("select max(gd.numOfGoodsOrder) from GoodsDealer gd"); Iterator max = query.iterate(); System.out.println("Maximum value of the property numOfGoodsOrder = "+max.next()); } catch(Exception e) { System.out.println(e.getMessage()); } finally { session.close(); } } }
Output :
1. goodsdealer table
2. When you will execute the java file HibernateMaxFunction.java ( RightClick -> Run As -> Java Application ) an output will displayed on console as :