Hibernate统计表中的条数

时间:2022-02-17 15:31:42


/**
 * 判断积分日志表中是否有某个用户的注册日志
 */
@Transactional(propagation = Propagation.REQUIRED)
public boolean isExistRegPointLog(int userId, UserAction addReg) {
 try {
  String hql = "select count(pl) from PointLog pl WHERE pl.userid = ? AND pl.userAction = ? ";

Object result = sessionFactory.getCurrentSession().createQuery(hql)
    .setParameter(0, userId).setParameter(1, addReg)
    .uniqueResult();

if ((Long) result > 0) {
   return true;
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 return false;
}