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;
}
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;
}
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;
}
Aggregations