看起来喜欢Hibernate没有这种语法,这是对的吗?
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;
}
}
答案 0 :(得分:11)
这有一些问题......
IFNULL
为COALESCE
(IFNULL equivalent in Hibernate Query Language?。)
list().get(0)
。session.createSqlQuery()
。IdentifierGenerator
(http://blog.anorakgirl.co.uk/2009/01/custom-hibernate-sequence-generator-for-id-field/)。无论如何,代码都是......
public int MaxIdenx() {
int max = (Integer)session
.createQuery("SELECT COALESCE(MAX(empId), 0) FROM Emp")
.uniqueResult();
return max + 1;
}