/**
   * There are 2 solutions: 1. DatabaseCollector#addTable()/getTable() should be called for not
   * quoted parameters - I think it is preferable way. 2. DatabaseCollector#addTable()/getTable()
   * should be called for quoted parameters - here users should use the same quotes as JDBCReader.
   * Because of this there are 2 opposite methods(and they are both failed as addTable uses quoted
   * names but getTable uses non-quoted names )
   */
  public void testQuotedNamesAndDefaultDatabaseCollector() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    ServiceRegistry serviceRegistry = builder.build();

    MetaDataDialect realMetaData =
        MetaDataDialectFactory.createMetaDataDialect(
            serviceRegistry.getService(JdbcServices.class).getDialect(), cfg.getProperties());

    JDBCReader reader =
        JDBCReaderFactory.newJDBCReader(
            cfg.getProperties(),
            new DefaultReverseEngineeringStrategy(),
            realMetaData,
            serviceRegistry);

    DatabaseCollector dc = new DefaultDatabaseCollector(reader.getMetaDataDialect());
    reader.readDatabaseSchema(dc, null, SCHEMA);

    assertNotNull("The table should be found", dc.getTable(SCHEMA, "PUBLIC", TABLE1));
    assertNotNull("The table should be found", dc.getTable(SCHEMA, "PUBLIC", TABLE2));
    assertNull(
        "Quoted names should not return the table", dc.getTable(quote(SCHEMA), "PUBLIC", QTABLE1));
    assertNull(
        "Quoted names should not return the table", dc.getTable(quote(SCHEMA), "PUBLIC", QTABLE2));

    assertEquals("Foreign key 'masterref' was filtered!", 1, dc.getOneToManyCandidates().size());
  }
Exemplo n.º 2
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);
    }
  }