Ejemplo n.º 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);
  }
Ejemplo n.º 2
0
  private String bindCollection(
      PersistentClass rc,
      ForeignKey fromForeignKey,
      ForeignKey toForeignKey,
      Collection collection) {
    ForeignKey targetKey = fromForeignKey;
    String collectionRole = null;
    boolean collectionLazy = false;
    boolean collectionInverse = false;
    TableIdentifier foreignKeyTable = null;
    String tableToClassName;

    if (toForeignKey != null) {
      targetKey = toForeignKey;
    }

    boolean uniqueReference =
        isUniqueReference(targetKey); // TODO: need to look one step further for many-to-many!
    foreignKeyTable = TableIdentifier.create(targetKey.getTable());
    TableIdentifier foreignKeyReferencedTable =
        TableIdentifier.create(targetKey.getReferencedTable());

    if (toForeignKey == null) {

      collectionRole =
          revengStrategy.foreignKeyToCollectionName(
              fromForeignKey.getName(),
              foreignKeyTable,
              fromForeignKey.getColumns(),
              foreignKeyReferencedTable,
              fromForeignKey.getReferencedColumns(),
              uniqueReference);

      tableToClassName = revengStrategy.tableToClassName(foreignKeyTable);
    } else {

      collectionRole =
          revengStrategy.foreignKeyToManyToManyName(
              fromForeignKey,
              TableIdentifier.create(fromForeignKey.getTable()),
              toForeignKey,
              uniqueReference);

      tableToClassName = revengStrategy.tableToClassName(foreignKeyReferencedTable);
    }

    collectionInverse =
        revengStrategy.isForeignKeyCollectionInverse(
            targetKey.getName(),
            foreignKeyTable,
            targetKey.getColumns(),
            foreignKeyReferencedTable,
            targetKey.getReferencedColumns());

    collectionLazy =
        revengStrategy.isForeignKeyCollectionLazy(
            targetKey.getName(),
            foreignKeyTable,
            targetKey.getColumns(),
            foreignKeyReferencedTable,
            targetKey.getReferencedColumns());

    collectionRole = makeUnique(rc, collectionRole);

    String fullRolePath = StringHelper.qualify(rc.getEntityName(), collectionRole);
    if (mappings.getCollection(fullRolePath) != null) {
      log.debug(fullRolePath + " found twice!");
    }

    collection.setRole(fullRolePath); // Master.setOfChildren+
    collection.setInverse(collectionInverse); // TODO: allow overriding this
    collection.setLazy(collectionLazy);
    collection.setFetchMode(FetchMode.SELECT);

    return tableToClassName;
  }
Ejemplo n.º 3
0
  /**
   * @param manyToOneCandidates
   * @param mappings2
   */
  private void createPersistentClasses(DatabaseCollector collector, Mapping mapping) {
    Map manyToOneCandidates = collector.getOneToManyCandidates();

    for (Iterator iter = mappings.iterateTables(); iter.hasNext(); ) {
      Table table = (Table) iter.next();
      if (table.getColumnSpan() == 0) {
        log.warn("Cannot create persistent class for " + table + " as no columns were found.");
        continue;
      }
      // TODO: this naively just create an entity per table
      // should have an opt-out option to mark some as helper tables, subclasses etc.
      /*if(table.getPrimaryKey()==null || table.getPrimaryKey().getColumnSpan()==0) {
      log.warn("Cannot create persistent class for " + table + " as no primary key was found.");
               continue;
               // TODO: just create one big embedded composite id instead.
           }*/

      if (revengStrategy.isManyToManyTable(table)) {
        log.debug("Ignoring " + table + " as class since rev.eng. says it is a many-to-many");
        continue;
      }

      RootClass rc = new RootClass();
      TableIdentifier tableIdentifier = TableIdentifier.create(table);
      String className = revengStrategy.tableToClassName(tableIdentifier);
      log.debug("Building entity " + className + " based on " + tableIdentifier);
      rc.setEntityName(className);
      rc.setClassName(className);
      rc.setProxyInterfaceName(rc.getEntityName()); // TODO: configurable ?
      rc.setLazy(true);

      rc.setMetaAttributes(safeMeta(revengStrategy.tableToMetaAttributes(tableIdentifier)));

      rc.setDiscriminatorValue(rc.getEntityName());
      rc.setTable(table);
      try {
        mappings.addClass(rc);
      } catch (DuplicateMappingException dme) {
        // TODO: detect this and generate a "permutation" of it ?
        PersistentClass class1 = mappings.getClass(dme.getName());
        Table table2 = class1.getTable();
        throw new JDBCBinderException(
            "Duplicate class name '"
                + rc.getEntityName()
                + "' generated for '"
                + table
                + "'. Same name where generated for '"
                + table2
                + "'");
      }
      mappings.addImport(rc.getEntityName(), rc.getEntityName());

      Set processed = new HashSet();

      PrimaryKeyInfo pki = bindPrimaryKeyToProperties(table, rc, processed, mapping, collector);
      bindColumnsToVersioning(table, rc, processed, mapping);
      bindOutgoingForeignKeys(table, rc, processed);
      bindColumnsToProperties(table, rc, processed, mapping);
      List incomingForeignKeys = (List) manyToOneCandidates.get(rc.getEntityName());
      bindIncomingForeignKeys(rc, processed, incomingForeignKeys, mapping);
      updatePrimaryKey(rc, pki);
    }
  }