Using Min, Max and Count on HQL
-
11-07-2019 - |
Question
Does hibernate HQL queries support using select min, max, count and other sql functions?
like:
select min(p.age) from person p
Thanks
Solution
Yes, min()
, max()
and count()
are supported in HQL.
see aggregate functions in the Hibernate Doc.
OTHER TIPS
thats how I am using max in Hibernate :
public long getNextId(){
long appId;
try{
Session session = HibernateUtil.getAdmSessionFactory().getCurrentSession();
Transaction t = session.beginTransaction();
String sequel = "Select max(JAdmAppExemptionId) from JAdmAppExemption";
Query q = session.createQuery(sequel);
List currentSeq = q.list();
if(currentSeq == null){
return appId;
}else{
appId = (Long)currentSeq.get(0);
return appId+1;
}
}catch(Exception exc){
System.out.print("Unable to get latestID");
exc.printStackTrace();
}
return 0;
}
Some aggregate functions are supported: look in the manual
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow