Search in sources :

Example 16 with ForeignKey

use of org.datanucleus.store.rdbms.key.ForeignKey in project datanucleus-rdbms by datanucleus.

the class SecondaryTable method getExpectedForeignKeys.

/**
 * Accessor for the expected foreign keys for this table.
 * @param clr ClassLoader resolver
 * @return The expected foreign keys.
 */
@Override
public List<ForeignKey> getExpectedForeignKeys(ClassLoaderResolver clr) {
    assertIsInitialized();
    // Auto mode allows us to decide which FKs are needed as well as using what is in the users MetaData.
    boolean autoMode = false;
    if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
        autoMode = true;
    }
    // Add FK back to the primary table unless requested not to
    List<ForeignKey> foreignKeys = new ArrayList<>();
    ForeignKeyMetaData fkmd = joinMetaData != null ? joinMetaData.getForeignKeyMetaData() : null;
    if (autoMode || (fkmd != null && fkmd.getDeleteAction() != ForeignKeyAction.NONE)) {
        ForeignKey fk = new ForeignKey(getIdMapping(), dba, primaryTable, fkmd != null && fkmd.isDeferred() ? true : false);
        if (fkmd != null && fkmd.getName() != null) {
            fk.setName(fkmd.getName());
        }
        foreignKeys.add(0, fk);
    }
    return foreignKeys;
}
Also used : ArrayList(java.util.ArrayList) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey)

Example 17 with ForeignKey

use of org.datanucleus.store.rdbms.key.ForeignKey in project datanucleus-rdbms by datanucleus.

the class TableImpl method getExpectedForeignKeys.

// ----------------------- Internal Implementation Methods -----------------
/**
 * Accessor for the expected foreign keys for this table in the datastore.
 * Currently only checks the columns for referenced tables (i.e relationships) and returns those.
 * @param clr The ClassLoaderResolver
 * @return List of foreign keys.
 */
public List<ForeignKey> getExpectedForeignKeys(ClassLoaderResolver clr) {
    assertIsInitialized();
    // The following Set is to avoid the duplicate usage of columns that have already been used in conjunction with another column
    Set<Column> colsInFKs = new HashSet<>();
    List<ForeignKey> foreignKeys = new ArrayList<>();
    Iterator i = columns.iterator();
    while (i.hasNext()) {
        Column col = (Column) i.next();
        if (!colsInFKs.contains(col)) {
            try {
                DatastoreClass referencedTable = storeMgr.getDatastoreClass(col.getStoredJavaType(), clr);
                if (referencedTable != null) {
                    for (int j = 0; j < col.getJavaTypeMapping().getNumberOfColumnMappings(); j++) {
                        colsInFKs.add(col.getJavaTypeMapping().getColumnMapping(j).getColumn());
                    }
                    ForeignKey fk = new ForeignKey(col.getJavaTypeMapping(), dba, referencedTable, true);
                    foreignKeys.add(fk);
                }
            } catch (NoTableManagedException e) {
            // expected when no table exists
            }
        }
    }
    return foreignKeys;
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) HashSet(java.util.HashSet)

Example 18 with ForeignKey

use of org.datanucleus.store.rdbms.key.ForeignKey in project datanucleus-rdbms by datanucleus.

the class TableImpl method getExpectedIndices.

/**
 * Accessor for the indices for this table in the datastore.
 * @param clr The ClassLoaderResolver
 * @return Set of indices expected.
 */
protected Set<Index> getExpectedIndices(ClassLoaderResolver clr) {
    assertIsInitialized();
    /*
         * For each foreign key, add to the list an index made up of the "from"
         * column(s) of the key, *unless* those columns also happen to be 
         * equal to the primary key (then they are indexed anyway).
         * Ensure that we have separate indices for foreign key columns 
         * if the primary key is the combination of foreign keys, e.g. in join tables.
         * This greatly decreases deadlock probability e.g. on Oracle.
         */
    Set<Index> indices = new HashSet<>();
    PrimaryKey pk = getPrimaryKey();
    Iterator<ForeignKey> i = getExpectedForeignKeys(clr).iterator();
    while (i.hasNext()) {
        ForeignKey fk = i.next();
        if (!pk.getColumnList().equals(fk.getColumnList())) {
            indices.add(new Index(fk));
        }
    }
    return indices;
}
Also used : PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) Index(org.datanucleus.store.rdbms.key.Index) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) HashSet(java.util.HashSet)

Aggregations

ForeignKey (org.datanucleus.store.rdbms.key.ForeignKey)18 ArrayList (java.util.ArrayList)9 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)5 Collection (java.util.Collection)4 NoTableManagedException (org.datanucleus.store.rdbms.exceptions.NoTableManagedException)4 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)3 List (java.util.List)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)2 EmbeddedPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping)2 Column (org.datanucleus.store.rdbms.table.Column)2