Java 类org.hibernate.mapping.ForeignKey 实例源码

项目:tipi-engine    文件:HibernateMetaDataTruncater.java   
@SuppressWarnings("unchecked")
private void addTableName(MetadataImplementor metadata, ArrayList<String> tables, Table table, String aSchemaName) {
    String name = (null == aSchemaName) ? "" : aSchemaName + ".";
    name += table.getName();
    if (tables.contains(name)) {
        return;
    }

    final Collection<Table> ts = metadata.collectTableMappings();
    for (Table t : ts) {
        if (t.equals(table)) {
            continue;
        }
        Iterator<ForeignKey> relationships = t.getForeignKeyIterator();
        while (relationships.hasNext()) {
            ForeignKey fk = relationships.next();
            if (fk.getReferencedTable().equals(table)) {
                addTableName(metadata, tables, fk.getTable(), aSchemaName);
            }
        }
    }
    tables.add(name);
}
项目:lams    文件:ForeignKeyMetadata.java   
public boolean matches(ForeignKey fk) {
    if ( refTable.equalsIgnoreCase( fk.getReferencedTable().getName() ) ) {
        if ( fk.getColumnSpan() == references.size() ) {
            List fkRefs;
            if ( fk.isReferenceToPrimaryKey() ) {
                fkRefs = fk.getReferencedTable().getPrimaryKey().getColumns();
            }
            else {
                fkRefs = fk.getReferencedColumns();
            }
            for ( int i = 0; i < fk.getColumnSpan(); i++ ) {
                Column column = fk.getColumn( i );
                Column ref = ( Column ) fkRefs.get( i );
                if ( !hasReference( column, ref ) ) {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}
项目:cacheonix-core    文件:PropertyRefTest.java   
public void testForeignKeyCreation() {
    PersistentClass classMapping = getCfg().getClassMapping("org.hibernate.test.propertyref.basic.Account");

    Iterator foreignKeyIterator = classMapping.getTable().getForeignKeyIterator();
    boolean found = false;
    while ( foreignKeyIterator.hasNext() ) {
        ForeignKey element = (ForeignKey) foreignKeyIterator.next();
        if(element.getReferencedEntityName().equals(Person.class.getName() ) ) {

            if(!element.isReferenceToPrimaryKey() ) {
                List referencedColumns = element.getReferencedColumns();
                Column column = (Column) referencedColumns.get(0);
                if(column.getName().equals("person_userid") ) {
                    found = true; // extend test to include the columns
                }               
            }
        }
    }

    assertTrue("Property ref foreign key not found",found);
}
项目:ODataSync    文件:ForeignKeyMetadata.java   
public boolean matches(ForeignKey fk) {
    if (refTable.equalsIgnoreCase(fk.getReferencedTable().getName())) {
        if (fk.getColumnSpan() == references.size()) {
            List fkRefs;
            if (fk.isReferenceToPrimaryKey()) {
                fkRefs = fk.getReferencedTable().getPrimaryKey().getColumns();
            } else {
                fkRefs = fk.getReferencedColumns();
            }

            for (int i = 0; i < fk.getColumnSpan(); i++) {
                Column column = fk.getColumn(i);
                Column ref = (Column)fkRefs.get(i);
                if (!hasReference(column, ref)) {
                    return false;
                }
            }

            return false;
        }
    }

    return false;
}
项目:lams    文件:Configuration.java   
protected void secondPassCompileForeignKeys(Table table, Set<ForeignKey> 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();
        }
    }
}
项目:lams    文件:TableMetadata.java   
public ForeignKeyMetadata getForeignKeyMetadata(ForeignKey fk) {
    Iterator it = foreignKeys.values().iterator();
    while ( it.hasNext() ) {
        ForeignKeyMetadata existingFk = ( ForeignKeyMetadata ) it.next();
        if ( existingFk.matches( fk ) ) {
            return existingFk;
        }
    }
    return null;
}
项目:cacheonix-core    文件:Configuration.java   
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"
                        );
                }
                if ( log.isDebugEnabled() ) {
                    log.debug( "resolving reference to class: " + referencedEntityName );
                }
                PersistentClass referencedClass = (PersistentClass) 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();
            }
        }
    }
项目:hibernate-conventions    文件:MappingConventions.java   
@SuppressWarnings("rawtypes")
private void normalizeForeignKeys(Table table, String entityName) {
    Iterator iterator = table.getForeignKeyIterator();
    while (iterator.hasNext()) {
        ForeignKey fk = (ForeignKey) iterator.next();
        String name = strategy.foreignKeyName(entityName, table.getName(),
                fk.getReferencedEntityName(), fk.getReferencedTable().getName());
        fk.setName(name);
    }
}
项目:hibernate-conventions    文件:MappingConventions.java   
@SuppressWarnings("rawtypes")
private void validateForeignKeys(Table table) {
    Iterator iterator = table.getForeignKeyIterator();
    while (iterator.hasNext()) {
        ForeignKey fk = (ForeignKey) iterator.next();
        validateMaxLength(fk.getName());
    }
}
项目:ODataSync    文件:TableMetadata.java   
public ForeignKeyMetadata getForeignKeyMetadata(ForeignKey fk) {
    Iterator iterator = foreignKeys.values().iterator();
    while (iterator.hasNext()) {
        ForeignKeyMetadata existingFk = (ForeignKeyMetadata)iterator.next();
        if (existingFk.matches(fk)) {
            return existingFk;
        }
    }

    return null;
}
项目:spanner-hibernate    文件:CloudSpannerDialect.java   
@Override
public String[] getSqlCreateStrings(ForeignKey exportable, Metadata metadata)
{
    return NO_COMMANDS;
}
项目:spanner-hibernate    文件:CloudSpannerDialect.java   
@Override
public String[] getSqlDropStrings(ForeignKey exportable, Metadata metadata)
{
    return NO_COMMANDS;
}
项目:spanner-hibernate    文件:CloudSpannerDialect.java   
@Override
public Exporter<ForeignKey> getForeignKeyExporter()
{
    return foreignKeyExporter;
}
项目:Equella    文件:HibernateMigrationHelper.java   
@SuppressWarnings({"unchecked"})
public List<String> getAddIndexesAndConstraintsForColumns(String tableName, boolean includeForeignKeyConstraints,
    String... columnNames)
{
    Set<Column> colSet = new HashSet<Column>();
    for( String columnName : columnNames )
    {
        colSet.add(new Column(columnName));
    }
    List<String> sqlStrings = new ArrayList<String>();

    Table table = findTable(tableName);
    if( !extDialect.supportsModifyWithConstraints() )
    {
        for( Column col : colSet )
        {
            Column realCol = table.getColumn(col);
            if( realCol.isUnique() )
            {
                table.createUniqueKey(Collections.singletonList(realCol));
            }
        }
    }
    Iterator<UniqueKey> keyIter = table.getUniqueKeyIterator();
    if( dialect.supportsUniqueConstraintInCreateAlterTable() )
    {
        while( keyIter.hasNext() )
        {
            UniqueKey uk = keyIter.next();
            if( !Collections.disjoint(uk.getColumns(), colSet) )
            {
                StringBuilder buf = new StringBuilder("alter table ");
                buf.append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema));
                buf.append(" add constraint ");
                buf.append(extDialect.getRandomIdentifier());
                buf.append(' ');
                String constraint = uk.sqlConstraintString(dialect);
                if( constraint != null )
                {
                    buf.append(constraint);
                    sqlStrings.add(buf.toString());
                }
            }
        }
    }
    else
    {
        while( keyIter.hasNext() )
        {
            UniqueKey ukey = keyIter.next();
            if( !Collections.disjoint(ukey.getColumns(), colSet) )
            {
                sqlStrings.add(ukey.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
            }
        }
    }

    addIndexSQL(sqlStrings, table, colSet);

    // Caller may opt to skip foreign key constraints
    if( includeForeignKeyConstraints )
    {
        Iterator<ForeignKey> fkeyIter = table.getForeignKeyIterator();
        while( fkeyIter.hasNext() )
        {
            ForeignKey fkey = fkeyIter.next();
            if( !Collections.disjoint(fkey.getColumns(), colSet) )
            {
                sqlStrings.add(fkey.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
            }
        }
    }

    return sqlStrings;
}
项目:Equella    文件:TablesOnlyFilter.java   
@Override
public boolean includeForeignKey(Table table, ForeignKey fk)
{
    return includeTable(table);
}
项目:Equella    文件:AllDataHibernateMigrationFilter.java   
@Override
public boolean includeForeignKey(Table table, ForeignKey fk)
{
    return true;
}
项目:AvoinApotti    文件:PatientMergerServlet.java   
/**
 * List Tables
 * <p>
 * This method is called to populate the tables hashtable. It reads all tables that have a foreign constraint to core_patient, excluding those tables specified in the exclude tables section of the configuration file.
 * 
 * @return 0 for or SUCCESS -1 for ERROR
 *         </p>
 */
private int listTables()
{
    tablesList = new MergeTableDetailsVoCollection();

    // Get list of entities with foreign constraints to Patient
    Iterator it = Registry.getInstance().getConfiguration().getClassMappings();
    while (it.hasNext())
    {
        PersistentClass cls = (PersistentClass) it.next();

        // If this is to be excluded, then continue
        if (pMergeConfig.isExcluded(cls.getClassName()))
                continue;
        Table tab = cls.getTable();
        Iterator it3= tab.getForeignKeyIterator();
        if (it3 != null)
        {
            while (it3.hasNext())
            {
                ForeignKey key = (ForeignKey) it3.next();
                if (key.getReferencedEntityName().equals(PATIENT_CLASS))
                {   
                    MergeTableDetailsVo tableDet = new MergeTableDetailsVo();
                    tableDet.setTableName(tab.getName());
                    tableDet.setColumnName(((Column)key.getColumns().get(0)).getName());
                    tableDet.setEntityName(cls.getClassName());
                    tableDet.setAttributeName(getAttributeForTabCol(cls.getClassName(), tableDet.getColumnName()));
                    tablesList.add(tableDet);
                }
            }
        }
    }

    localLogger.debug("Tables List for MERGE...");
    for (int i=0; i<tablesList.size(); i++)
    {
        localLogger.debug("" + (i+1) + "Table:" + tablesList.get(i).getTableName() + "  Col:" +
                    tablesList.get(i).getColumnName() + " Entity:" + tablesList.get(i).getEntityName() + 
                    " Att:" + tablesList.get(i).getAttributeName());
    }
    return tablesList.size();
}
项目:openMAXIMS    文件:PatientMergerServlet.java   
/**
 * List Tables
 * <p>
 * This method is called to populate the tables hashtable. It reads all tables that have a foreign constraint to core_patient, excluding those tables specified in the exclude tables section of the configuration file.
 * 
 * @return 0 for or SUCCESS -1 for ERROR
 *         </p>
 */
private int listTables()
{
    tablesList = new MergeTableDetailsVoCollection();

    // Get list of entities with foreign constraints to Patient
    Iterator it = Registry.getInstance().getConfiguration().getClassMappings();
    while (it.hasNext())
    {
        PersistentClass cls = (PersistentClass) it.next();

        // If this is to be excluded, then continue
        if (pMergeConfig.isExcluded(cls.getClassName()))
                continue;
        Table tab = cls.getTable();
        Iterator it3= tab.getForeignKeyIterator();
        if (it3 != null)
        {
            while (it3.hasNext())
            {
                ForeignKey key = (ForeignKey) it3.next();
                if (key.getReferencedEntityName().equals(PATIENT_CLASS))
                {   
                    MergeTableDetailsVo tableDet = new MergeTableDetailsVo();
                    tableDet.setTableName(tab.getName());
                    tableDet.setColumnName(((Column)key.getColumns().get(0)).getName());
                    tableDet.setEntityName(cls.getClassName());
                    tableDet.setAttributeName(getAttributeForTabCol(cls.getClassName(), tableDet.getColumnName()));
                    tablesList.add(tableDet);
                }
            }
        }
    }

    localLogger.debug("Tables List for MERGE...");
    for (int i=0; i<tablesList.size(); i++)
    {
        localLogger.debug("" + (i+1) + "Table:" + tablesList.get(i).getTableName() + "  Col:" +
                    tablesList.get(i).getColumnName() + " Entity:" + tablesList.get(i).getEntityName() + 
                    " Att:" + tablesList.get(i).getAttributeName());
    }
    return tablesList.size();
}
项目:openMAXIMS    文件:PatientMergerServlet.java   
/**
 * List Tables
 * <p>
 * This method is called to populate the tables hashtable. It reads all tables that have a foreign constraint to core_patient, excluding those tables specified in the exclude tables section of the configuration file.
 * 
 * @return 0 for or SUCCESS -1 for ERROR
 *         </p>
 */
private int listTables()
{
    tablesList = new MergeTableDetailsVoCollection();

    // Get list of entities with foreign constraints to Patient
    Iterator it = Registry.getInstance().getConfiguration().getClassMappings();
    while (it.hasNext())
    {
        PersistentClass cls = (PersistentClass) it.next();

        // If this is to be excluded, then continue
        if (pMergeConfig.isExcluded(cls.getClassName()))
                continue;
        Table tab = cls.getTable();
        Iterator it3= tab.getForeignKeyIterator();
        if (it3 != null)
        {
            while (it3.hasNext())
            {
                ForeignKey key = (ForeignKey) it3.next();
                if (key.getReferencedEntityName().equals(PATIENT_CLASS))
                {   
                    MergeTableDetailsVo tableDet = new MergeTableDetailsVo();
                    tableDet.setTableName(tab.getName());
                    tableDet.setColumnName(((Column)key.getColumns().get(0)).getName());
                    tableDet.setEntityName(cls.getClassName());
                    tableDet.setAttributeName(getAttributeForTabCol(cls.getClassName(), tableDet.getColumnName()));
                    tablesList.add(tableDet);
                }
            }
        }
    }

    localLogger.debug("Tables List for MERGE...");
    for (int i=0; i<tablesList.size(); i++)
    {
        localLogger.debug("" + (i+1) + "Table:" + tablesList.get(i).getTableName() + "  Col:" +
                    tablesList.get(i).getColumnName() + " Entity:" + tablesList.get(i).getEntityName() + 
                    " Att:" + tablesList.get(i).getAttributeName());
    }
    return tablesList.size();
}
项目:openmaxims-linux    文件:PatientMergerServlet.java   
/**
 * List Tables
 * <p>
 * This method is called to populate the tables hashtable. It reads all tables that have a foreign constraint to core_patient, excluding those tables specified in the exclude tables section of the configuration file.
 * 
 * @return 0 for or SUCCESS -1 for ERROR
 *         </p>
 */
private int listTables()
{
    tablesList = new MergeTableDetailsVoCollection();

    // Get list of entities with foreign constraints to Patient
    Iterator it = Registry.getInstance().getConfiguration().getClassMappings();
    while (it.hasNext())
    {
        PersistentClass cls = (PersistentClass) it.next();

        // If this is to be excluded, then continue
        if (pMergeConfig.isExcluded(cls.getClassName()))
                continue;
        Table tab = cls.getTable();
        Iterator it3= tab.getForeignKeyIterator();
        if (it3 != null)
        {
            while (it3.hasNext())
            {
                ForeignKey key = (ForeignKey) it3.next();
                if (key.getReferencedEntityName().equals(PATIENT_CLASS))
                {   
                    MergeTableDetailsVo tableDet = new MergeTableDetailsVo();
                    tableDet.setTableName(tab.getName());
                    tableDet.setColumnName(((Column)key.getColumns().get(0)).getName());
                    tableDet.setEntityName(cls.getClassName());
                    tableDet.setAttributeName(getAttributeForTabCol(cls.getClassName(), tableDet.getColumnName()));
                    tablesList.add(tableDet);
                }
            }
        }
    }

    localLogger.debug("Tables List for MERGE...");
    for (int i=0; i<tablesList.size(); i++)
    {
        localLogger.debug("" + (i+1) + "Table:" + tablesList.get(i).getTableName() + "  Col:" +
                    tablesList.get(i).getColumnName() + " Entity:" + tablesList.get(i).getEntityName() + 
                    " Att:" + tablesList.get(i).getAttributeName());
    }
    return tablesList.size();
}
项目:lightblue-rdbms    文件:SimpleSQLMappingTranslator.java   
@Override
public void translate(TranslatorContext tc) {
    DatabaseCollector collector = tc.getDatabaseCollector();
    Map<String, TableIdentifier> mapped = new HashMap<>();
    RDBMS rdbms = setupRDBMS(tc);

    rdbms.setDialect((String) tc.getMap().get("rdbmsDialect"));

    for (Iterator<Table> i = collector.iterateTables(); i.hasNext();) {
        Table table = i.next();
        if(table.getColumnSpan()==0) {
            LOGGER.warn("Table without column found and it will be ignored. Its name is '" + table + "'.");
            continue;
        }

        Map<String, TableIdentifier> fks = new HashMap<>();

        TableIdentifier tableIdentifier = TableIdentifier.create(table);
        String id = tableIdentifier.toString();

        if(!mapped.containsKey(id)) {
            mapped.put(id, tableIdentifier);
        } else {
            throw new IllegalStateException("Table mapped twice");
        }


        Join join = new Join();
        join.setProjectionMappings(new ArrayList<ProjectionMapping>());
        join.setTables(new ArrayList<com.redhat.lightblue.metadata.rdbms.model.Table>());

        for (Iterator<ForeignKey> j =  table.getForeignKeyIterator(); j.hasNext();) {
            ForeignKey fk = j.next();
            Table referencedTable = fk.getReferencedTable();
            TableIdentifier ti = TableIdentifier.create(referencedTable);
            for (Iterator<Column> z =  fk.getColumns().iterator(); z.hasNext();) {
                Column c =  z.next();
                fks.put(c.getName(),ti);
            }
        }


        Boolean mapfk = (Boolean) tc.getMap().get("mapfk");
        for (Iterator<Column> j = table.getColumnIterator(); j.hasNext();) {
            Column column = j.next();
            if(fks.get(column.getName())== null || mapfk ){
                ColumnToField field = setupColumnToField(table, column);
                rdbms.getSQLMapping().getColumnToFieldMap().add(field);

                ProjectionMapping projectionMapping = setupProjectionMapping(column);
                join.getProjectionMappings().add(projectionMapping);
            }
        }

        com.redhat.lightblue.metadata.rdbms.model.Table rdbmTable = new com.redhat.lightblue.metadata.rdbms.model.Table();
        rdbmTable.setName(table.getName());
        join.getTables().add(rdbmTable);

        rdbms.getSQLMapping().getJoins().add(join);
    }
}
项目:lightblue-rdbms    文件:JoinedTablesSQLMappingTranslator.java   
@Override
public void translate(TranslatorContext tc) {
    DatabaseCollector collector = tc.getDatabaseCollector();
    Map<String, TableIdentifier> mapped = new HashMap<>();
    RDBMS rdbms = setupRDBMS(tc);
    Map<String, Join> tableToJoin = new HashMap<>();
    Map<TableIdentifier,PrimaryKey> pks = new HashMap<>();

    rdbms.setDialect((String) tc.getMap().get("rdbmsDialect"));

    for (Iterator<Table> i = collector.iterateTables(); i.hasNext();) {
        Table table = i.next();
        if(table.getColumnSpan()==0) {
            LOGGER.warn("Table without column found and it will be ignored. Its name is '" + table + "'.");
            continue;
        }
        /*
        // TODO analyze this case
        if(revengStrategy.isManyToManyTable(table)) {}
        */

        Map<String, ForeignKey> fks = new HashMap<>();

        TableIdentifier tableIdentifier = TableIdentifier.create(table);
        String id = tableIdentifier.toString();

        pks.put(tableIdentifier,table.getPrimaryKey());

        if(!mapped.containsKey(id)) {
            mapped.put(id, tableIdentifier);
        } else {
            throw new IllegalStateException("Table mapped twice");
        }

        Join join = null;
        if(tableToJoin.get(id) == null){
            join = new Join();
            join.setProjectionMappings(new ArrayList<ProjectionMapping>());
            join.setTables(new ArrayList<com.redhat.lightblue.metadata.rdbms.model.Table>());
            join.setJoinTablesStatement("");
            rdbms.getSQLMapping().getJoins().add(join);
            tableToJoin.put(id,join);
        }else{
            join = tableToJoin.get(id);
        }

        for (Iterator<ForeignKey> j =  table.getForeignKeyIterator(); j.hasNext();) {
            ForeignKey fk = j.next();
            Table referencedTable = fk.getReferencedTable();
            TableIdentifier ti = TableIdentifier.create(referencedTable);
            tableToJoin.put(ti.toString(),join);
            for (Iterator<Column> z =  fk.getColumns().iterator(); z.hasNext();) {
                Column c =  z.next();
                fks.put(c.getName(),fk);
                String joinTable = join.getJoinTablesStatement();
                if(joinTable.length() !=  0){
                    joinTable = joinTable + " AND ";
                }
                join.setJoinTablesStatement(joinTable + table.getName() + "." + c.getName() + "=" + referencedTable.getName() + "." + c.getName());
            }
        }

        for (Iterator<Column> j = table.getColumnIterator(); j.hasNext();) {
            Column column = j.next();
            if(fks.get(column.getName())== null ){
                ColumnToField field = setupColumnToField(table, column);
                rdbms.getSQLMapping().getColumnToFieldMap().add(field);

                ProjectionMapping projectionMapping = setupProjectionMapping(column);
                join.getProjectionMappings().add(projectionMapping);
            }
        }

        com.redhat.lightblue.metadata.rdbms.model.Table rdbmTable = new com.redhat.lightblue.metadata.rdbms.model.Table();
        rdbmTable.setName(table.getName());
        join.getTables().add(rdbmTable);
    }
}
项目:Equella    文件:HibernateCreationFilter.java   
boolean includeForeignKey(Table table, ForeignKey fk);