如何在Hibernate中使用mysql的语法,如ifnull(max(Id),0)

时间:2012-11-20 07:17:30

标签: java mysql hibernate

看起来喜欢Hibernate没有这种语法,这是对的吗?enter image description here

public int MaxIdenx() {
    int max = 0;
    String hql = "select ifnull(max(empId),0)from Emp";
    Query query = session.createQuery(hql);
    List currentSeq = query.list();
    if (currentSeq == null) {
        return max;
    } else {
        max = (Integer) currentSeq.get(0);
        return max + 1;
    }
}

1 个答案:

答案 0 :(得分:11)

这有一些问题......

无论如何,代码都是......

public int MaxIdenx() {
    int max = (Integer)session
        .createQuery("SELECT COALESCE(MAX(empId), 0) FROM Emp")
        .uniqueResult();

    return max + 1;
}