2016-12-30 59 views
0

我有表,跟隨着屬性使用Hibernate

h_id,姓名,檢索表最大值.....

,我想回發生在表中最大編號。

我使用

"select max(h.h_id) from Hospital h" 

會拋出

org.hibernate.exception.ConstraintViolationException:無法 執行語句

什麼riht辦法做到這一點?

//編輯,代碼

public static Integer getHospitalId(){ 
     List<Integer> ids = null; 
     try { 
      Query q = session.createQuery ("select max(h.h_id) from Hospital h"); 
      ids = (List<Integer>)q.list(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     if(ids!= null) 
      return ids.get(0) + 1; 
     return new Integer(0); 
    } 
} 
+0

你確定這是導致限制衝突查詢?也許如果你分享了一些代碼和完整的堆棧跟蹤,你可以得到更好的答案。 –

+0

添加代碼示例 – Dingo

+0

爲什麼使用'list()'? –

回答

0

你試試這樣說:

SELECT max(h_id) FROM Hospital