Esempio n. 1
0
  /**
   * @param table
   * @param rc
   * @param primaryKey
   */
  private void bindColumnsToProperties(
      Table table, RootClass rc, Set processedColumns, Mapping mapping) {

    for (Iterator iterator = table.getColumnIterator(); iterator.hasNext(); ) {
      Column column = (Column) iterator.next();
      if (!processedColumns.contains(column)) {
        checkColumn(column);

        String propertyName =
            revengStrategy.columnToPropertyName(TableIdentifier.create(table), column.getName());

        Property property =
            bindBasicProperty(
                makeUnique(rc, propertyName), table, column, processedColumns, mapping);

        rc.addProperty(property);
      }
    }
  }
Esempio n. 2
0
  private void bindVersionProperty(
      Table table,
      TableIdentifier identifier,
      Column column,
      RootClass rc,
      Set processed,
      Mapping mapping) {

    processed.add(column);
    String propertyName = revengStrategy.columnToPropertyName(identifier, column.getName());
    Property property =
        bindBasicProperty(makeUnique(rc, propertyName), table, column, processed, mapping);
    rc.addProperty(property);
    rc.setVersion(property);
    rc.setOptimisticLockMode(Versioning.OPTIMISTIC_LOCK_VERSION);
    log.debug(
        "Column "
            + column.getName()
            + " will be used for <version>/<timestamp> columns in "
            + identifier);
  }
Esempio n. 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;
  }
Esempio n. 4
0
  private PrimaryKeyInfo bindPrimaryKeyToProperties(
      Table table, RootClass rc, Set processed, Mapping mapping, DatabaseCollector collector) {
    SimpleValue id = null;
    String idPropertyname = null;

    PrimaryKeyInfo pki = new PrimaryKeyInfo();

    List keyColumns = null;
    if (table.getPrimaryKey() != null) {
      keyColumns = table.getPrimaryKey().getColumns();
    } else {
      log.debug("No primary key found for " + table + ", using all properties as the identifier.");
      keyColumns = new ArrayList();
      Iterator iter = table.getColumnIterator();
      while (iter.hasNext()) {
        Column col = (Column) iter.next();
        keyColumns.add(col);
      }
    }

    final TableIdentifier tableIdentifier = TableIdentifier.create(table);

    String tableIdentifierStrategyName = "assigned";

    boolean naturalId;

    if (keyColumns.size() > 1) {
      log.debug(
          "id strategy for " + rc.getEntityName() + " since it has a multiple column primary key");
      naturalId = true;

      id = handleCompositeKey(rc, processed, keyColumns, mapping);
      idPropertyname = revengStrategy.tableToIdentifierPropertyName(tableIdentifier);
      if (idPropertyname == null) {
        idPropertyname = "id";
      }
    } else {
      pki.suggestedStrategy = revengStrategy.getTableIdentifierStrategyName(tableIdentifier);
      String suggestedStrategy = pki.suggestedStrategy;
      if (suggestedStrategy == null) {
        suggestedStrategy =
            collector.getSuggestedIdentifierStrategy(
                tableIdentifier.getCatalog(),
                tableIdentifier.getSchema(),
                tableIdentifier.getName());
        if (suggestedStrategy == null) {
          suggestedStrategy = "assigned";
        }
        tableIdentifierStrategyName = suggestedStrategy;
      } else {
        tableIdentifierStrategyName = suggestedStrategy;
      }

      naturalId = "assigned".equals(tableIdentifierStrategyName);
      Column pkc = (Column) keyColumns.get(0);
      checkColumn(pkc);

      id = bindColumnToSimpleValue(table, pkc, mapping, !naturalId);

      idPropertyname = revengStrategy.tableToIdentifierPropertyName(tableIdentifier);
      if (idPropertyname == null) {
        idPropertyname = revengStrategy.columnToPropertyName(tableIdentifier, pkc.getName());
      }

      processed.add(pkc);
    }
    id.setIdentifierGeneratorStrategy(tableIdentifierStrategyName);
    pki.suggestedProperties = revengStrategy.getTableIdentifierProperties(tableIdentifier);
    id.setIdentifierGeneratorProperties(pki.suggestedProperties);
    if (naturalId) {
      id.setNullValue("undefined");
    }

    Property property =
        makeProperty(
            tableIdentifier, makeUnique(rc, idPropertyname), id, true, true, false, null, null);
    rc.setIdentifierProperty(property);
    rc.setIdentifier(id);

    return pki;
  }