Hibernate从表中获取具有最大字段值的单行

时间:2010-08-26 15:28:05

标签: java hibernate orm hql

在Hibernate中,如何从表中获取具有最大字段值的单行?

由于

1 个答案:

答案 0 :(得分:2)

Using Custom Query

表格结构:

  

ID insurance_name invest_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());
    }
  }
}  

来自here