Exemple #1
0
  private Property bindOneToOne(
      PersistentClass rc,
      Table targetTable,
      ForeignKey fk,
      Set processedColumns,
      boolean constrained,
      boolean inverseProperty) {

    OneToOne value = new OneToOne(targetTable, rc);
    value.setReferencedEntityName(
        revengStrategy.tableToClassName(TableIdentifier.create(targetTable)));

    boolean isUnique = isUniqueReference(fk);
    String propertyName = null;
    if (inverseProperty) {
      propertyName =
          revengStrategy.foreignKeyToInverseEntityName(
              fk.getName(),
              TableIdentifier.create(fk.getReferencedTable()),
              fk.getReferencedColumns(),
              TableIdentifier.create(targetTable),
              fk.getColumns(),
              isUnique);
    } else {
      propertyName =
          revengStrategy.foreignKeyToEntityName(
              fk.getName(),
              TableIdentifier.create(fk.getReferencedTable()),
              fk.getReferencedColumns(),
              TableIdentifier.create(targetTable),
              fk.getColumns(),
              isUnique);
    }

    Iterator columns = fk.getColumnIterator();
    while (columns.hasNext()) {
      Column fkcolumn = (Column) columns.next();
      checkColumn(fkcolumn);
      value.addColumn(fkcolumn);
      processedColumns.add(fkcolumn);
    }

    value.setFetchMode(FetchMode.SELECT);

    value.setConstrained(constrained);
    value.setForeignKeyType(
        constrained
            ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT
            : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT);

    return makeEntityProperty(propertyName, true, targetTable, fk, value, inverseProperty);
    // return makeProperty(TableIdentifier.create(targetTable), propertyName, value,
    //        true, true, value.getFetchMode() != FetchMode.JOIN, null, null);
  }
Exemple #2
0
  /**
   * bind many-to-ones
   *
   * @param table
   * @param rc
   * @param primaryKey
   */
  private void bindOutgoingForeignKeys(Table table, RootClass rc, Set processedColumns) {

    // Iterate the outgoing foreign keys and create many-to-one's
    for (Iterator iterator = table.getForeignKeyIterator(); iterator.hasNext(); ) {
      ForeignKey foreignKey = (ForeignKey) iterator.next();

      boolean mutable = true;
      if (contains(foreignKey.getColumnIterator(), processedColumns)) {
        if (!cfg.preferBasicCompositeIds()) continue; // it's in the pk, so skip this one
        mutable = false;
      }

      if (revengStrategy.excludeForeignKeyAsManytoOne(
          foreignKey.getName(),
          TableIdentifier.create(foreignKey.getTable()),
          foreignKey.getColumns(),
          TableIdentifier.create(foreignKey.getReferencedTable()),
          foreignKey.getReferencedColumns())) {
        // TODO: if many-to-one is excluded should the column be marked as processed so it won't
        // show up at all ?
        log.debug("Rev.eng excluded *-to-one for foreignkey " + foreignKey.getName());
      } else if (revengStrategy.isOneToOne(foreignKey)) {
        Property property =
            bindOneToOne(
                rc, foreignKey.getReferencedTable(), foreignKey, processedColumns, true, false);
        rc.addProperty(property);
      } else {
        boolean isUnique = isUniqueReference(foreignKey);
        String propertyName =
            revengStrategy.foreignKeyToEntityName(
                foreignKey.getName(),
                TableIdentifier.create(foreignKey.getTable()),
                foreignKey.getColumns(),
                TableIdentifier.create(foreignKey.getReferencedTable()),
                foreignKey.getReferencedColumns(),
                isUnique);

        Property property =
            bindManyToOne(
                makeUnique(rc, propertyName), mutable, table, foreignKey, processedColumns);

        rc.addProperty(property);
      }
    }
  }
Exemple #3
0
  /**
   * Basically create an [classname]Id.class and add properties for it.
   *
   * @param rc
   * @param compositeKeyColumns
   * @param processed
   * @return
   */
  private SimpleValue handleCompositeKey(
      RootClass rc, Set processedColumns, List keyColumns, Mapping mapping) {
    Component pkc = new Component(rc);
    pkc.setMetaAttributes(Collections.EMPTY_MAP);
    pkc.setEmbedded(false);

    String compositeIdName =
        revengStrategy.tableToCompositeIdName(TableIdentifier.create(rc.getTable()));
    if (compositeIdName == null) {
      compositeIdName = revengStrategy.classNameToCompositeIdName(rc.getClassName());
    }
    pkc.setComponentClassName(compositeIdName);
    Table table = rc.getTable();
    List list = null;
    if (cfg.preferBasicCompositeIds()) {
      list = new ArrayList(keyColumns);
    } else {
      list = findForeignKeys(table.getForeignKeyIterator(), keyColumns);
    }
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
      Object element = iter.next();
      Property property;
      if (element instanceof Column) {
        Column column = (Column) element;
        if (processedColumns.contains(column)) {
          throw new JDBCBinderException(
              "Binding column twice for primary key should not happen: " + column);
        } else {
          checkColumn(column);

          String propertyName =
              revengStrategy.columnToPropertyName(TableIdentifier.create(table), column.getName());
          property =
              bindBasicProperty(
                  makeUnique(pkc, propertyName), table, column, processedColumns, mapping);

          processedColumns.add(column);
        }
      } else if (element instanceof ForeignKeyForColumns) {
        ForeignKeyForColumns fkfc = (ForeignKeyForColumns) element;
        ForeignKey foreignKey = fkfc.key;
        String propertyName =
            revengStrategy.foreignKeyToEntityName(
                foreignKey.getName(),
                TableIdentifier.create(foreignKey.getTable()),
                foreignKey.getColumns(),
                TableIdentifier.create(foreignKey.getReferencedTable()),
                foreignKey.getReferencedColumns(),
                true);
        property =
            bindManyToOne(makeUnique(pkc, propertyName), true, table, foreignKey, processedColumns);
        processedColumns.addAll(fkfc.columns);
      } else {
        throw new JDBCBinderException("unknown thing");
      }

      markAsUseInEquals(property);
      pkc.addProperty(property);
    }

    return pkc;
  }