コード例 #1
0
ファイル: MetamodelImpl.java プロジェクト: krossell/BatooJPA
  /**
   * Performs the foreign key DDL operations.
   *
   * @param datasource the datasource
   * @param ddlMode the DDL Mode
   * @param entity the entity to perform DDL against
   * @throws BatooException thrown in case of an underlying exception
   * @since $version
   * @author hceylan
   */
  public void performForeignKeysDdl(
      DataSource datasource, DDLMode ddlMode, EntityTypeImpl<?> entity) {
    if ((ddlMode == DDLMode.NONE)) {
      return;
    }

    MetamodelImpl.LOG.info(
        "Performing foreign key DDL operations for entiy {0}, mode {1}", entity.getName(), ddlMode);

    for (final EntityTable table : entity.getTables()) {
      // skip parent tables
      if (table.getEntity() != entity) {
        continue;
      }

      MetamodelImpl.LOG.info(
          "Performing foreign key DDL operations for table {0}, mode {1}",
          table.getQName(), ddlMode);

      for (final ForeignKey foreignKey : table.getForeignKeys()) {
        this.jdbcAdaptor.createForeignKey(datasource, foreignKey);
      }
    }

    for (final AssociationMapping<?, ?, ?> mapping : entity.getAssociations()) {
      final JoinTable table = mapping.getTable();
      // skip not applicable join tables
      if ((table == null) || (table.getEntity() != entity)) {
        continue;
      }

      MetamodelImpl.LOG.info(
          "Performing foreign key DDL operations for join table {0}, mode {1}",
          table.getQName(), ddlMode);

      for (final ForeignKey foreignKey : table.getForeignKeys()) {
        this.jdbcAdaptor.createForeignKey(datasource, foreignKey);
      }
    }

    for (final PluralMapping<?, ?, ?> mapping : entity.getMappingsPlural()) {
      if (!mapping.isAssociation()) {
        final AbstractTable table = (AbstractTable) mapping.getTable();
        MetamodelImpl.LOG.info(
            "Performing foreign key DDL operations for join table {0}, mode {1}",
            table.getQName(), ddlMode);

        for (final ForeignKey foreignKey : table.getForeignKeys()) {
          this.jdbcAdaptor.createForeignKey(datasource, foreignKey);
        }
      }
    }
  }