@Override public ArrayList<User> getAllUsers() throws Exception { dLog.info("Entering method getAllUsers"); ArrayList<User> result = new ArrayList<User>(); Session session = null; try { session = getSession(); session.clear(); Transaction tranx = session.beginTransaction(); String hql = "select from User"; Query query = session.createQuery(hql); List<?> list = query.list(); for (int n = 0; n < list.size(); n++) { result.add((User) list.get(n)); } tranx.commit(); session.flush(); session.evict(User.class); } catch (Exception e) { dLog.error("Exception in getAllUsers", e); } finally { // ensure that session is close regardless of the errors in try/catch if (session != null) { session.close(); } } return result; }
@Override public boolean deleteUser(Integer id) throws Exception { dLog.info("Entering method deleteUser | User ID:" + id); boolean result = false; Session session = null; try { session = getSession(); String hql = "delete from User where id = " + id.toString(); Query query = session.createQuery(hql); int row = query.executeUpdate(); if (row > 0) { result = true; } } catch (Exception e) { dLog.error("Exception in deleteUser", e); } finally { // ensure that session is close regardless of the errors in try/catch if (session != null) { session.close(); } } return result; }