Exemple #1
0
  @SuppressWarnings("unchecked")
  public static <T> T getEntity(
      final DbUtils db, final Cursor cursor, Class<T> entityType, long findCacheSequence) {
    if (db == null || cursor == null) return null;

    EntityTempCache.setSeq(findCacheSequence);
    try {
      Table table = Table.get(db, entityType);
      Id id = table.id;
      String idColumnName = id.getColumnName();
      int idIndex = id.getIndex();
      if (idIndex < 0) {
        idIndex = cursor.getColumnIndex(idColumnName);
      }
      Object idValue = id.getColumnConverter().getFieldValue(cursor, idIndex);
      T entity = EntityTempCache.get(entityType, idValue);
      if (entity == null) {
        entity = entityType.newInstance();
        id.setValue2Entity(entity, cursor, idIndex);
        EntityTempCache.put(entityType, idValue, entity);
      } else {
        return entity;
      }
      int columnCount = cursor.getColumnCount();
      for (int i = 0; i < columnCount; i++) {
        String columnName = cursor.getColumnName(i);
        Column column = table.columnMap.get(columnName);
        if (column != null) {
          column.setValue2Entity(entity, cursor, i);
        }
      }

      // init finder
      for (Finder finder : table.finderMap.values()) {
        finder.setValue2Entity(entity, null, 0);
      }
      return entity;
    } catch (Throwable e) {
      LogUtils.e(e.getMessage(), e);
    }

    return null;
  }
Exemple #2
0
 @SuppressWarnings("unchecked")
 public static <T> T get(Class<T> entityType, Object idValue) {
   return (T) cache.get(entityType.getName() + "#" + idValue);
 }
Exemple #3
0
 public static <T> void put(Class<T> entityType, Object idValue, Object entity) {
   cache.put(entityType.getName() + "#" + idValue, entity);
 }