コード例 #1
0
  @Override
  protected Foo removeImpl(Foo foo) {
    foo = toUnwrappedModel(foo);

    Session session = null;

    try {
      session = openSession();

      if (!session.contains(foo)) {
        foo = (Foo) session.get(FooImpl.class, foo.getPrimaryKeyObj());
      }

      if (foo != null) {
        session.delete(foo);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    if (foo != null) {
      clearCache(foo);
    }

    return foo;
  }
コード例 #2
0
  @Override
  public Map<Serializable, Foo> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
      return Collections.emptyMap();
    }

    Map<Serializable, Foo> map = new HashMap<Serializable, Foo>();

    if (primaryKeys.size() == 1) {
      Iterator<Serializable> iterator = primaryKeys.iterator();

      Serializable primaryKey = iterator.next();

      Foo foo = fetchByPrimaryKey(primaryKey);

      if (foo != null) {
        map.put(primaryKey, foo);
      }

      return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
      Foo foo =
          (Foo)
              EntityCacheUtil.getResult(
                  FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, primaryKey);

      if (foo == null) {
        if (uncachedPrimaryKeys == null) {
          uncachedPrimaryKeys = new HashSet<Serializable>();
        }

        uncachedPrimaryKeys.add(primaryKey);
      } else {
        map.put(primaryKey, foo);
      }
    }

    if (uncachedPrimaryKeys == null) {
      return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_FOO_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
      query.append(String.valueOf(primaryKey));

      query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
      session = openSession();

      Query q = session.createQuery(sql);

      for (Foo foo : (List<Foo>) q.list()) {
        map.put(foo.getPrimaryKeyObj(), foo);

        cacheResult(foo);

        uncachedPrimaryKeys.remove(foo.getPrimaryKeyObj());
      }

      for (Serializable primaryKey : uncachedPrimaryKeys) {
        EntityCacheUtil.putResult(
            FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, primaryKey, _nullFoo);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    return map;
  }