Example #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("\\.", "_");
   }
 }
Example #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;
  }
 private <E, T> boolean keepMapping(Collection<E> col1, Collection<T> col2)
     throws IllegalAccessException, InstantiationException {
   Class claxx1 = col1.iterator().next().getClass();
   Class claxx2 = col2.iterator().next().getClass();
   EntityTable table1 = TableManager.getTable(claxx1);
   EntityTable table2 = TableManager.getTable(claxx2);
   if (table1.mappingList != null) {
     for (MapProperty mp : table1.mappingList) {
       Class itemClass;
       Class fieldClass = mp.field.getType();
       if (mp.isToMany()) {
         // N对多关系
         if (ClassUtil.isCollection(fieldClass)) {
           itemClass = FieldUtil.getGenericType(mp.field);
         } else {
           throw new RuntimeException(
               "OneToMany and ManyToMany Relation, You must use collection object");
         }
       } else {
         itemClass = fieldClass;
       }
       if (itemClass == claxx2) {
         ArrayList<String> key1List = new ArrayList<String>();
         HashMap<String, Object> map1 = new HashMap<String, Object>();
         // 构建第1个对象的key集合以及value映射
         for (Object o1 : col1) {
           if (o1 != null) {
             Object key1 = FieldUtil.get(table1.key.field, o1);
             if (key1 != null) {
               key1List.add(key1.toString());
               map1.put(key1.toString(), o1);
             }
           }
         }
         ArrayList<Relation> mapList = queryRelation(claxx1, claxx2, key1List, null);
         if (!Checker.isEmpty(mapList)) {
           HashMap<String, Object> map2 = new HashMap<String, Object>();
           // 构建第2个对象的value映射
           for (Object o2 : col2) {
             if (o2 != null) {
               Object key2 = FieldUtil.get(table2.key.field, o2);
               if (key2 != null) {
                 map2.put(key2.toString(), o2);
               }
             }
           }
           for (Relation m : mapList) {
             Object obj1 = map1.get(m.key1);
             Object obj2 = map2.get(m.key2);
             if (obj1 != null && obj2 != null) {
               if (mp.isToMany()) {
                 // N对多关系
                 if (ClassUtil.isCollection(fieldClass)) {
                   Collection col = (Collection) FieldUtil.get(mp.field, obj1);
                   if (col == null) {
                     col = (Collection) fieldClass.newInstance();
                     FieldUtil.set(mp.field, obj1, col);
                   }
                   col.add(obj2);
                 } else {
                   throw new RuntimeException(
                       "OneToMany and ManyToMany Relation, You must use collection object");
                 }
               } else {
                 FieldUtil.set(mp.field, obj1, obj2);
               }
             }
           }
           return true;
         }
       }
     }
   }
   return false;
 }