Esempio n. 1
0
 public HasOneField getHasOneField(String name) {
   for (HasOneField field : hasOneFields) {
     if (field.getName().equals(name)) {
       return field;
     }
   }
   return null;
 }
Esempio n. 2
0
  public static OrmInfo getOrmInfo(Class<?> clasz) {
    OrmInfo orm = new OrmInfo();

    Table t = clasz.getAnnotation(Table.class);
    if (t != null) {
      orm.table = t.name();
    }

    List<ColumnField> columnFields = new ArrayList<ColumnField>();
    List<HasManyField> hasManyFields = new ArrayList<HasManyField>();
    List<HasOneField> hasOneFields = new ArrayList<HasOneField>();
    List<BelongsToField> belongsToFields = new ArrayList<BelongsToField>();

    for (Field f : clasz.getDeclaredFields()) {
      Id id = f.getAnnotation(Id.class);
      if (id != null) {
        orm.id = f.getName();
        orm.idGeneratorType = id.generate();
        orm.idType = f.getType();
      }
      Column column = f.getAnnotation(Column.class);
      if (column != null) {
        ColumnField field = new ColumnField();
        field.setName(f.getName());
        field.setType(f.getType());
        columnFields.add(field);
      }

      HasMany hasMany = f.getAnnotation(HasMany.class);
      if (hasMany != null) {
        HasManyField field = new HasManyField();
        field.setName(f.getName());
        field.setAnnotation(hasMany);
        ParameterizedType ptype = (ParameterizedType) f.getGenericType();
        Class<?> childClass = (Class<?>) ptype.getActualTypeArguments()[0];
        if (hasMany.foreignKey().equals("")) {
          field.setForeignKey(orm.table + "_id");
        } else {
          field.setForeignKey(hasMany.foreignKey());
        }
        field.setTargetType(childClass);

        hasManyFields.add(field);
      }
      HasOne hasOne = f.getAnnotation(HasOne.class);
      if (hasOne != null) {
        HasOneField field = new HasOneField();
        field.setName(f.getName());
        field.setAnnotation(hasOne);
        field.setTargetType(f.getType());
        if (hasOne.foreignKey().equals("")) {
          field.setForeignKey(orm.table + "_id");
        } else {
          field.setForeignKey(hasOne.foreignKey());
        }

        hasOneFields.add(field);
      }
      BelongsTo belongsTo = f.getAnnotation(BelongsTo.class);
      if (belongsTo != null) {
        BelongsToField field = new BelongsToField();
        field.setName(f.getName());
        field.setAnnotation(belongsTo);
        field.setTargetType(f.getType());
        if (belongsTo.foreignKey().equals("")) {
          OrmInfo targetOrm = OrmInfo.getOrmInfo(f.getType());
          field.setForeignKey(targetOrm.table + "_id");
        } else {
          field.setForeignKey(belongsTo.foreignKey());
        }
        belongsToFields.add(field);
      }
    }

    orm.columnFields = columnFields.toArray(new ColumnField[columnFields.size()]);
    orm.hasManyFields = hasManyFields.toArray(new HasManyField[hasManyFields.size()]);
    orm.hasOneFields = hasOneFields.toArray(new HasOneField[hasOneFields.size()]);
    orm.belongsToFields = belongsToFields.toArray(new BelongsToField[belongsToFields.size()]);
    return orm;
  }