【问题标题】:Retrieve max value from table using hibernate使用休眠从表中检索最大值
【发布时间】: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 我正在逐步指导教程,我也找不到没有列表的方法
  • 您正在查找表Hospitalh_id 列的最大值。 Hospital 是一个实体吗?如果是,那么您应该确保h_id 是类变量名,或者更改查询以匹配类变量名。

标签: java hibernate


【解决方案1】:

你试过这种方式吗:

SELECT max(h_id) FROM Hospital

?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多