Ejemplo n.º 1
0
 /** 根据类自动生成表名字 */
 public static String getTableName(Class<?> claxx) {
   Table anno = claxx.getAnnotation(Table.class);
   if (anno != null) {
     return anno.value();
   } else {
     return claxx.getName().replaceAll("\\.", "_");
   }
 }
Ejemplo n.º 2
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;
  }