2016-12-30 45 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