Package org.hibernate.mapping

Examples of org.hibernate.mapping.ForeignKey


  }

  private boolean isAppliedToForeignColumns(Table table, Constraint constraint) {
    List<?> constraintColumns = constraint.getColumns();
    for ( Iterator<?> iterator = table.getForeignKeyIterator(); iterator.hasNext(); ) {
      ForeignKey foreignKey = (ForeignKey) iterator.next();
      List<?> foreignKeyColumns = foreignKey.getColumns();
      for ( Object object : foreignKeyColumns ) {
        if ( constraintColumns.contains( object ) ) {
          // This constraint requires a foreign column
          return true;
        }
View Full Code Here


      while ( itr.hasNext() ) {
        Table table = (Table) itr.next();
        if ( table.isPhysicalTable() ) {
          Iterator subItr = table.getForeignKeyIterator();
          while ( subItr.hasNext() ) {
            ForeignKey fk = (ForeignKey) subItr.next();
            if ( fk.isPhysicalConstraint() ) {
              script.add(
                  fk.sqlDropString(
                      dialect,
                      defaultCatalog,
                      defaultSchema
                    )
                );
View Full Code Here

        }

        if ( dialect.hasAlterTable() ) {
          subIter = table.getForeignKeyIterator();
          while ( subIter.hasNext() ) {
            ForeignKey fk = (ForeignKey) subIter.next();
            if ( fk.isPhysicalConstraint() ) {
              script.add(
                  fk.sqlCreateString(
                      dialect, mapping,
                      defaultCatalog,
                      defaultSchema
                    )
                );
View Full Code Here

          );

        if ( dialect.hasAlterTable() ) {
          Iterator subIter = table.getForeignKeyIterator();
          while ( subIter.hasNext() ) {
            ForeignKey fk = (ForeignKey) subIter.next();
            if ( fk.isPhysicalConstraint() ) {
              boolean create = tableInfo == null || (
                  tableInfo.getForeignKeyMetadata( fk ) == null && (
                      //Icky workaround for MySQL bug:
                      !( dialect instanceof MySQLDialect ) ||
                          tableInfo.getIndexMetadata( fk.getName() ) == null
                    )
                );
              if ( create ) {
                script.add(
                    fk.sqlCreateString(
                        dialect,
                        mapping,
                        defaultCatalog,
                        defaultSchema
                      )
View Full Code Here

  protected void secondPassCompileForeignKeys(Table table, Set done) throws MappingException {
    table.createForeignKeys();
    Iterator iter = table.getForeignKeyIterator();
    while ( iter.hasNext() ) {

      ForeignKey fk = (ForeignKey) iter.next();
      if ( !done.contains( fk ) ) {
        done.add( fk );
        final String referencedEntityName = fk.getReferencedEntityName();
        if ( referencedEntityName == null ) {
          throw new MappingException(
              "An association from the table " +
              fk.getTable().getName() +
              " does not specify the referenced entity"
            );
        }
                LOG.debugf("Resolving reference to class: %s", referencedEntityName);
        PersistentClass referencedClass = classes.get( referencedEntityName );
        if ( referencedClass == null ) {
          throw new MappingException(
              "An association from the table " +
              fk.getTable().getName() +
              " refers to an unmapped class: " +
              referencedEntityName
            );
        }
        if ( referencedClass.isJoinedSubclass() ) {
          secondPassCompileForeignKeys( referencedClass.getSuperclass().getTable(), done );
        }
        fk.setReferencedTable( referencedClass.getTable() );
        fk.alignColumns();
      }
    }
  }
View Full Code Here

  // bind collections.
  private void bindIncomingForeignKeys(PersistentClass rc, Set processed, List foreignKeys, Mapping mapping) {
    if(foreignKeys!=null) {
      for (Iterator iter = foreignKeys.iterator(); iter.hasNext();) {
        ForeignKey foreignKey = (ForeignKey) iter.next();

        if(revengStrategy.excludeForeignKeyAsCollection(
            foreignKey.getName(),
            TableIdentifier.create(foreignKey.getTable() ),
            foreignKey.getColumns(),
            TableIdentifier.create(foreignKey.getReferencedTable() ),
            foreignKey.getReferencedColumns())) {
          log.debug("Rev.eng excluded one-to-many or one-to-one for foreignkey " + foreignKey.getName());
        } else if (revengStrategy.isOneToOne(foreignKey)){
          Property property = bindOneToOne(rc, foreignKey.getTable(), foreignKey, processed, false, true);
          rc.addProperty(property);
        } else {
          Property property = bindOneToMany(rc, foreignKey, processed, mapping);
          rc.addProperty(property);
        }
View Full Code Here

          if(keys.size()>1) {
            throw new JDBCBinderException("more than one other foreign key to choose from!"); // todo: handle better ?
          }

          ForeignKey fk = (ForeignKey) keys.get( 0 );

          String tableToClassName = bindCollection( rc, foreignKey, fk, collection );

      element.setReferencedEntityName( tableToClassName );
      element.addColumn( fk.getColumn( 0 ) );
      collection.setElement( element );

        } else {
          String tableToClassName = bindCollection( rc, foreignKey, null, collection );
View Full Code Here

  }


  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) {
View Full Code Here

  /** return true if this foreignkey is the only reference from this table to the same foreign table */
    private boolean isUniqueReference(ForeignKey foreignKey) {

      Iterator foreignKeyIterator = foreignKey.getTable().getForeignKeyIterator();
      while ( foreignKeyIterator.hasNext() ) {
      ForeignKey element = (ForeignKey) foreignKeyIterator.next();
      if(element!=foreignKey && element.getReferencedTable().equals(foreignKey.getReferencedTable())) {
        return false;
      }
    }
    return true;
  }
View Full Code Here

   */
  private void bindOutgoingForeignKeys(Table table, RootClass rc, Set processedColumns) {

    // Iterate the outgoing foreign keys and create many-to-one's
    for(Iterator iterator = table.getForeignKeyIterator(); iterator.hasNext();) {
      ForeignKey foreignKey = (ForeignKey) iterator.next();

      boolean mutable = true;
            if ( contains( foreignKey.getColumnIterator(), processedColumns ) ) {
        if ( !cfg.preferBasicCompositeIds() ) continue; //it's in the pk, so skip this one
        mutable = false;
            }

            if(revengStrategy.excludeForeignKeyAsManytoOne(foreignKey.getName(),
              TableIdentifier.create(foreignKey.getTable() ),
              foreignKey.getColumns(),
              TableIdentifier.create(foreignKey.getReferencedTable() ),
              foreignKey.getReferencedColumns())) {
              // TODO: if many-to-one is excluded should the column be marked as processed so it won't show up at all ?
              log.debug("Rev.eng excluded *-to-one for foreignkey " + foreignKey.getName());
            } else if (revengStrategy.isOneToOne(foreignKey)){
        Property property = bindOneToOne(rc, foreignKey.getReferencedTable(), foreignKey, processedColumns, true, false);
        rc.addProperty(property);
      } else {
              boolean isUnique = isUniqueReference(foreignKey);
              String propertyName = revengStrategy.foreignKeyToEntityName(
                  foreignKey.getName(),
                  TableIdentifier.create(foreignKey.getTable() ),
                  foreignKey.getColumns(),
                  TableIdentifier.create(foreignKey.getReferencedTable() ),
                  foreignKey.getReferencedColumns(),
                  isUnique
              );

              Property property = bindManyToOne(
                  makeUnique(rc, propertyName),
View Full Code Here

TOP

Related Classes of org.hibernate.mapping.ForeignKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.