示例#1
0
  private void setPrimaryKeyMetaData(Map<Object, Object> context, final JavaClassSource entity) {
    String pkName = "id";
    String pkType = "Long";
    String nullablePkType = "Long";
    for (MemberSource<JavaClassSource, ?> m : entity.getMembers()) {
      if (m.hasAnnotation(Id.class)) {
        if (m instanceof Field) {
          Field<?> field = (Field<?>) m;
          pkName = field.getName();
          pkType = field.getType().getQualifiedName();
          nullablePkType = pkType;
          break;
        }

        MethodSource<?> method = (MethodSource<?>) m;
        pkName = method.getName().substring(3);
        if (method.getName().startsWith("get")) {
          pkType = method.getReturnType().getQualifiedName();
        } else {
          pkType = method.getParameters().get(0).getType().getQualifiedName();
        }
        nullablePkType = pkType;
        break;
      }
    }

    if (Types.isJavaLang(pkType)) {
      nullablePkType = Types.toSimpleName(pkType);
    } else if ("int".equals(pkType)) {
      nullablePkType = Integer.class.getSimpleName();
    } else if ("short".equals(pkType)) {
      nullablePkType = Short.class.getSimpleName();
    } else if ("byte".equals(pkType)) {
      nullablePkType = Byte.class.getSimpleName();
    } else if ("long".equals(pkType)) {
      nullablePkType = Long.class.getSimpleName();
    }

    context.put("primaryKey", pkName);
    context.put("primaryKeyCC", StringUtils.capitalize(pkName));
    context.put("primaryKeyType", pkType);
    context.put("nullablePrimaryKeyType", nullablePkType);
  }