示例#1
0
  /**
   * No checking on the annotations - client could get unpredictable behaviour if annotations aren't
   * configured properly.
   *
   * @return
   */
  public void discover() {

    jpaEntity = entityClass.isAnnotationPresent(javax.persistence.Entity.class);
    Table table = (Table) entityClass.getAnnotation(Table.class);
    if (!jpaEntity || table == null)
      throw new UnsupportedOperationException(); // throw a more appropriate exception

    tableName = table.name();

    // Absolutely no validation annotation parsing!!
    // this will ALWAYS parse annotations in this order: ID, EmbeddedId, and Column

    Field[] fields = entityClass.getDeclaredFields();
    for (Field f : fields) {

      // first Id annotation
      Annotation fan = f.getAnnotation(Id.class);
      if (fan != null) {
        Column col = (Column) f.getAnnotation(Column.class);
        idMapping = new ColumnMapping(col.name(), f);
        continue;
      }

      // Embedded ID
      fan = f.getAnnotation(EmbeddedId.class);
      if (fan != null) {

        ArrayList<ColumnMapping> emList = new ArrayList<>();

        for (Field inF : f.getType().getDeclaredFields()) {
          if (inF.getAnnotation(Column.class) != null) {
            Column innerId = (Column) inF.getAnnotation(Column.class);
            // put column name in lower case as driver does
            emList.add(new ColumnMapping(innerId.name().toLowerCase(), inF));
          }
        }
        // column name
        embedded = new Embedded(f, emList.toArray(emptyColArr));

        continue;
      }
      // finally get the column
      // there's special handling for map type - which is a definite area for improvement

      fan = f.getAnnotation(Column.class);
      if (fan != null) {
        Column col = (Column) fan;
        ColumnMapping mapping = new ColumnMapping(col.name(), f);
        // special consideration for map
        if (Map.class.isAssignableFrom(f.getType())) {
          mapping.isMap = true;
        }
        // put in lower case as the driver
        colsToFields.put(col.name().toLowerCase(), mapping);

        continue;
      }
    }
  }
示例#2
0
  /**
   * Populates and iterates through a {@link Row} using {@link ColumnDefinitions} provided by the
   * driver.
   *
   * @param row
   * @return
   */
  public T get(Row row) {

    // Entity and its associated id object
    T entity = null;
    Object idObj = null;

    try {
      entity = entityClass.newInstance();
      idObj = null;
      ColumnMapping idcolmap = null;
      // Note that this early invocation means that
      // you can't populate on #setId in your java bean method
      if (embedded != null) {
        idObj = embedded.field.getType().newInstance();
        embedded.set(entity, idObj);
      } else {
        idObj = idMapping.field.getType().newInstance();
        idMapping.set(entity, idObj);
      }

    } catch (InstantiationException | IllegalAccessException e) {
      throw new IllegalAccessError(
          "A configuration exception has occurred in creating entity: " + entityClass);
    }
    ColumnDefinitions metaData = row.getColumnDefinitions();
    List<Definition> defList = metaData.asList();

    for (Definition def : defList) {
      ColumnMapping mapping = colsToFields.get(def.getName());

      if (mapping == null) {

        // it could be an id column
        if (idMapping != null && def.getName().equals(idMapping.name)) {
          idMapping.set(entity, getValue(row, idMapping, def));
          continue;
        }

        // else: need to find it, possible point of refactor
        ColumnMapping nembed = null;
        if (embedded != null) nembed = embedded.get(def.getName());
        if (nembed != null) nembed.set(idObj, getValue(row, nembed, def));

        continue;
      }

      Object value = getValue(row, mapping, def);
      // set it
      if (value != null) mapping.set(entity, value);
    }
    return entity;
  }