コード例 #1
0
  @Override
  public boolean storeUser(User u) throws Exception {
    dLog.info("Entering method storeUser | User ID: " + u.getId());
    boolean result = false;
    Session session = null;

    try {
      // ensure we were passed a valid object before attempting to write
      if (u.validate()) {
        session = getSession();
        Transaction tranx = session.beginTransaction();
        session.save(u);
        tranx.commit();
        result = tranx.wasCommitted();
        dLog.info("Was committed: " + result);
      }
    } catch (Exception e) {
      dLog.error("Exception in storeUser", e);
    } finally {
      // ensure that session is close regardless of the errors in try/catch
      if (session != null) {
        session.close();
      }
    }

    return result;
  }
コード例 #2
0
  @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;
  }
コード例 #3
0
  @Override
  public User getUser(String userName) throws Exception {
    dLog.info("Entering method getUser | User name: " + userName);
    User u = null;
    Session session = null;

    try {
      session = getSession();
      Transaction tranx = session.beginTransaction();
      u = (User) session.get(User.class, userName);
      tranx.commit();
    } catch (Exception e) {
      dLog.error("Exception in getUser", e);
    } finally {
      // ensure that session is close regardless of the errors in try/catch
      if (session != null) {
        session.close();
      }
    }

    return u;
  }