Exemple #1
0
  /** 批量操作对象:添加、修改、删除 */
  private static void optEntities(final List<?> entities, final int opt) throws Exception {
    if (entities == null || entities.isEmpty()) {
      return;
    }

    Object idf = null;
    Session session = null;
    Transaction tx = null;
    try {
      idf = HibernateUtil.createSession();
      session = HibernateUtil.getSession();
      tx = session.beginTransaction();

      Object entity;
      for (int i = 0, len = entities.size(); i < len; i++) {
        entity = entities.get(i);
        if (entity == null) {
          continue;
        }

        switch (opt) {
          case OPT_SAVE:
            session.save(entity);
            break;
          case OPT_UPDATE:
            session.update(entity);
            break;
          case OPT_DELETE:
            session.delete(entity);
            break;
          default:
            break;
        }

        if ((i + 1) % 50 == 0) {
          session.flush();
          session.clear();
        }
      }

      session.flush();
      session.clear();
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
        session.clear();
      }

      throw error(".optEntities()", e);
    } finally {
      if (idf != null && session != null) {
        closeQuickly(idf);
      }
    }
  }
Exemple #2
0
  /** 操作对象:添加、修改、删除 */
  private static Serializable optEntity(final Object entity, final int opt) throws Exception {
    if (entity == null) {
      return null;
    }

    Object idf = null;
    Session session = null;
    Transaction tx = null;
    Serializable id = null;
    try {
      idf = HibernateUtil.createSession();
      session = HibernateUtil.getSession();
      tx = session.beginTransaction();

      switch (opt) {
        case OPT_SAVE:
          id = session.save(entity);
          break;
        case OPT_UPDATE:
          session.update(entity);
          break;
        case OPT_DELETE:
          session.delete(entity);
          break;
        default:
          break;
      }

      session.flush();
      session.clear();
      tx.commit();
      return id;
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
        session.clear();
      }

      throw error(".optEntity()", e);
    } finally {
      if (idf != null && session != null) {
        closeQuickly(idf);
      }
    }
  }
Exemple #3
0
  // --------------------------------
  // TODO Worker/save/delete/update
  // --------------------------------
  public static <Params, Result> Result doWorker(
      Worker<Params, Result> worker, Params... initParams) throws BLException, Exception {
    if (worker == null) {
      return null;
    }

    Object idf = null;
    Session session = null;
    Transaction tx = null;
    try {
      idf = HibernateUtil.createSession();
      session = HibernateUtil.getSession();
      tx = session.beginTransaction();

      Result res = worker.execute(session, initParams);

      session.flush();
      session.clear();
      tx.commit();
      return res;
    } catch (BLException e) {
      if (tx != null) {
        tx.rollback();
        session.clear();
      }

      throw e;
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
        session.clear();
      }

      throw error(".doWorker()", e);
    } finally {
      if (idf != null && session != null) {
        closeQuickly(idf);
      }
    }
  }