【发布时间】:2016-12-30 20:16:06
【问题描述】:
我有以下属性的表格
h_id,名字,.....
我想返回表中出现的最大 ID。
我正在使用
"select max(h.h_id) from Hospital h"
抛出
org.hibernate.exception.ConstraintViolationException: 不能 执行语句
正确的做法是什么?
//编辑,代码
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);
}
}
【问题讨论】:
-
您确定这是导致约束违规的查询吗?也许如果您共享一些代码和完整的堆栈跟踪,您可以获得更好的答案。
-
添加代码示例
-
你为什么使用
list()? -
我正在使用列表 bcs 我正在逐步指导教程,我也找不到没有列表的方法
-
您正在查找表
Hospital中h_id列的最大值。Hospital是一个实体吗?如果是,那么您应该确保h_id是类变量名,或者更改查询以匹配类变量名。