Exemple #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;
      }
    }
  }