Пример #1
0
  /** 获取缓存实体表信息 */
  public EntityTable getEntityTable(Class<?> claxx, boolean needPK) {
    EntityTable table = getEntityTable(claxx.getName());
    if (table == null) {
      table = new EntityTable();
      putEntityTable(claxx.getName(), table);
      table.claxx = claxx;
      table.name = TableUtil.getTableName(claxx);
      table.pmap = new LinkedHashMap<String, Property>();
      List<Field> fields = FieldUtil.getAllDeclaredFields(claxx);
      // Field[] fields = claxx.getDeclaredFields();
      for (Field f : fields) {
        if (FieldUtil.isInvalid(f)) {
          continue;
        }
        Property p = new Property();
        p.field = f;
        // 获取列名,每个属性都有,没有注解默认取属性名
        Column col = f.getAnnotation(Column.class);
        if (col != null) {
          p.column = col.value();
        } else {
          p.column = f.getName();
        }

        // 主键判断
        PrimaryKey key = f.getAnnotation(PrimaryKey.class);
        if (key != null) {
          // 主键不加入属性Map
          table.key = new com.litesuits.orm.db.model.PrimaryKey(p, key.value());
          // 主键为系统分配,必须为Long型
          if (table.key.isAssignedBySystem()) {
            if (table.key.field.getType() != Long.class
                && table.key.field.getType() != long.class) {
              throw new RuntimeException(
                  PrimaryKey.AssignType.AUTO_INCREMENT
                      + "要求主键属性必须是Long型( the primary key should be Long or long...)\n 提示:把你的主键设置为Long或long型");
            }
          }
        } else {
          // ORM handle
          Mapping mapping = f.getAnnotation(Mapping.class);
          if (mapping != null) {
            table.addMapping(new MapProperty(p, mapping.value()));
          } else {
            table.pmap.put(p.column, p);
          }
        }
      }
    }
    if (needPK && table.key == null) {
      throw new RuntimeException(
          "你必须设置主键(you must set the primary key...)\n 提示:在对象的属性上加PrimaryKey注解来设置主键。");
    }
    return table;
  }
Пример #2
0
 /**
  * 获取映射表信息(Entity Table) 注意映射表存储在MAP中,key 为 database name + table name, value 为 entity table。
  *
  * @return {@link EntityTable}
  */
 private EntityTable getMappingTable(String tableName, String column1, String column2) {
   EntityTable table = getEntityTable(dbName + tableName);
   if (table == null) {
     table = new EntityTable();
     table.name = tableName;
     table.pmap = new LinkedHashMap<String, Property>();
     table.pmap.put(column1, null);
     table.pmap.put(column2, null);
     TableManager.putEntityTable(dbName + tableName, table);
   }
   return table;
 }
Пример #3
0
  /**
   * 获取实体表信息(Entity Table) 注意映射表存储在MAP中,key 为 class name, value 为 entity table。
   *
   * @return {@link EntityTable}
   */
  public static synchronized EntityTable getTable(Class<?> claxx, boolean needPK) {
    if (claxx == null) {
      return null;
    }
    EntityTable table = getEntityTable(claxx.getName());
    // if(Log.isPrint)Log.i(TAG, "table : " + table + "  , claxx: " + claxx);
    if (table == null) {
      table = new EntityTable();
      putEntityTable(claxx.getName(), table);
      table.claxx = claxx;
      table.name = getTableName(claxx);
      table.pmap = new LinkedHashMap<String, Property>();
      List<Field> fields = FieldUtil.getAllDeclaredFields(claxx);
      // Field[] fields = claxx.getDeclaredFields();
      for (Field f : fields) {
        if (FieldUtil.isInvalid(f)) {
          continue;
        }
        Property p = new Property();
        p.field = f;
        // 获取列名,每个属性都有,没有注解默认取属性名
        // if(Log.isPrint)Log.i(TAG, "Column : " + Column.class+ "  field: "+ f);
        Column col = f.getAnnotation(Column.class);
        if (col != null) {
          p.column = col.value();
        } else {
          p.column = f.getName();
        }

        // 主键判断
        // if(Log.isPrint)Log.i(TAG, "PrimaryKey : " + PrimaryKey.class + "  field: "+ f + " asst:"
        // + AssignType.AUTO_INCREMENT);
        PrimaryKey key = f.getAnnotation(PrimaryKey.class);
        if (key != null) {
          // 主键不加入属性Map
          table.key = new com.litesuits.orm.db.model.PrimaryKey(p, key.value());
          // 主键为系统分配,对类型有要求
          if (table.key.isAssignedBySystem()) {
            if (!FieldUtil.isLong(table.key.field) && !FieldUtil.isInteger(table.key.field)) {
              throw new RuntimeException(
                  PrimaryKey.AssignType.AUTO_INCREMENT
                      + "要求主键属性必须是long或者int( the primary key should be long or int...)\n "
                      + "提示:把你的主键设置为long或int型");
            }
          }
        } else {
          // ORM handle
          // if(Log.isPrint)Log.i(TAG, "Mapping : " + Mapping.class+ " field: "+ f);
          Mapping mapping = f.getAnnotation(Mapping.class);
          if (mapping != null) {
            table.addMapping(new MapProperty(p, mapping.value()));
          } else {
            table.pmap.put(p.column, p);
          }
        }
      }
    }
    if (needPK && table.key == null) {
      throw new RuntimeException(
          "你必须为["
              + table.claxx.getSimpleName()
              + "]设置主键(you must set the primary key...)"
              + "\n 提示:在对象的属性上加PrimaryKey注解来设置主键。");
    }
    return table;
  }