开发者

Hibernate get single row from the table with maximum field value

开发者 https://www.devze.com 2023-01-13 11:43 出处:网络
In Hibe开发者_开发技巧rnate, how can I 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号