Hibernate get single row from the table with maximum field value
In Hibe开发者_开发技巧rnate, how can I get single row from the table with maximum field value?
Thanks
Using Custom Query
Table Structure:
ID insurance_name invested_amount investement_date
public class HibernateHQLMaxFunction {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try {
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();
String SQL_QUERY = "select
max(FIELD_NAME)from Insurance insurance";
Query query = sess.createQuery(SQL_QUERY);
List list = query.list();
System.out.println("Max
Invested Amount: " + list.get(0));
sess.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
from here
精彩评论