Esempio n. 1
0
  private void updatePrimaryKey(RootClass rc, PrimaryKeyInfo pki) {
    SimpleValue idValue = (SimpleValue) rc.getIdentifierProperty().getValue();

    Properties defaultStrategyProperties = new Properties();
    Property constrainedOneToOne = getConstrainedOneToOne(rc);
    if (constrainedOneToOne != null) {
      if (pki.suggestedStrategy == null) {
        idValue.setIdentifierGeneratorStrategy("foreign");
      }

      if (pki.suggestedProperties == null) {
        defaultStrategyProperties.setProperty("property", constrainedOneToOne.getName());
        idValue.setIdentifierGeneratorProperties(defaultStrategyProperties);
      }
    }
  }
Esempio n. 2
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;
  }