else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr) != null &&
memberMapping.getNumberOfDatastoreMappings() > 0 &&
memberMapping instanceof PersistableMapping)
{
// Field is for a PC class with the FK at this side, so add a FK to the table of this PC
ForeignKey fk = TableUtils.getForeignKeyForPCField(memberMapping, mmd, autoMode, storeMgr, clr);
if (fk != null)
{
foreignKeys.add(fk);
}
}
}
}
// FK from id column(s) to id column(s) of superclass, as specified by
// <inheritance><join><foreign-key ...></join></inheritance>
ForeignKeyMetaData idFkmd = (cmd.getInheritanceMetaData().getJoinMetaData() != null) ?
cmd.getInheritanceMetaData().getJoinMetaData().getForeignKeyMetaData() : null;
if (supertable != null && (autoMode || (idFkmd != null && idFkmd.getDeleteAction() != ForeignKeyAction.NONE)))
{
ForeignKey fk = new ForeignKey(getIdMapping(), dba, supertable, false);
if (idFkmd != null && idFkmd.getName() != null)
{
fk.setName(idFkmd.getName());
}
foreignKeys.add(0, fk);
}
// Add any user-required FKs for the class as a whole
// <class><foreign-key>...</foreign-key></field>
Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
while (cmdIter.hasNext())
{
AbstractClassMetaData thisCmd = cmdIter.next();
ForeignKeyMetaData[] fkmds = thisCmd.getForeignKeyMetaData();
if (fkmds != null)
{
for (int i=0;i<fkmds.length;i++)
{
ForeignKey fk = getForeignKeyForForeignKeyMetaData(fkmds[i]);
if (fk != null)
{
foreignKeys.add(fk);
}
}
}
}
HashMap externalFks = getExternalFkMappings();
if (!externalFks.isEmpty())
{
// 1-N FK relationships - FK to id column(s) of owner table where this is the element table and we have a FK
Collection externalFkKeys = externalFks.entrySet();
Iterator<Map.Entry<AbstractMemberMetaData, JavaTypeMapping>> externalFkKeysIter = externalFkKeys.iterator();
while (externalFkKeysIter.hasNext())
{
Map.Entry<AbstractMemberMetaData, JavaTypeMapping> entry = externalFkKeysIter.next();
AbstractMemberMetaData fmd = entry.getKey();
DatastoreClass referencedTable = storeMgr.getDatastoreClass(fmd.getAbstractClassMetaData().getFullClassName(), clr);
if (referencedTable != null)
{
// Take <foreign-key> from either <field> or <element>
ForeignKeyMetaData fkmd = fmd.getForeignKeyMetaData();
if (fkmd == null && fmd.getElementMetaData() != null)
{
fkmd = fmd.getElementMetaData().getForeignKeyMetaData();
}
if ((fkmd != null && fkmd.getDeleteAction() != ForeignKeyAction.NONE) || autoMode)
{
// Either has been specified by user, or using autoMode, so add FK
JavaTypeMapping fkMapping = entry.getValue();
ForeignKey fk = new ForeignKey(fkMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd); // Does nothing when no FK MetaData
if (!foreignKeys.contains(fk))
{
// Only add when not already present (in the case of shared FKs there can be dups here)
foreignKeys.add(fk);
}