views:

18

answers:

1

Hi Guys,

In Hibernate, how can I get single row from the table with maximum field value?

Thanks

A: 

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

org.life.java