我一直在尝试使用hibernate计算行数,但我一直在计算" 0"记录。这是我的代码
public static Integer getLastDrugId(){
Integer result = 0;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
String hql = "SELECT count(*) FROM Drug";
Query query = session.createQuery(hql);
Integer e = (Integer) query.iterate().next();
if (e != null) {
result = 1;
}
} catch (Exception ex) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return result;
}
我在某处弄错了吗?
答案 0 :(得分:1)
替换此行:
Integer e = (Integer) query.iterate().next();
这个:
Long e = (Long) query.iterate().next();