{
fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode)
{
ForeignKey fk = new ForeignKey(ownerMapping,dba,referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
if (!isSerialisedValuePC())
{
if (isEmbeddedValuePC())
{
// Add any FKs for the fields of the (embedded) value
EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping)valueMapping;
for (int i=0;i<embMapping.getNumberOfJavaTypeMappings();i++)
{
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
if (ClassUtils.isReferenceType(embFmd.getType()) &&
embFieldMapping instanceof ReferenceMapping)
{
// Field is a reference type, so add a FK to the table of the PC for each PC implementation
Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd,
autoMode, storeMgr, clr);
foreignKeys.addAll(fks);
}
else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null &&
embFieldMapping.getNumberOfDatastoreMappings() > 0 &&
embFieldMapping 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(embFieldMapping, embFmd,
autoMode, storeMgr, clr);
if (fk != null)
{
foreignKeys.add(fk);
}
}
}
}
else if (mmd.getMap().valueIsPersistent())
{
// FK from join table to value table
referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
if (referencedTable != null)
{
// Take <foreign-key> from <value>
ForeignKeyMetaData fkmd = null;
if (mmd.getValueMetaData() != null)
{
fkmd = mmd.getValueMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode)
{
ForeignKey fk = new ForeignKey(valueMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
}
}
if (!isSerialisedKeyPC())
{
if (isEmbeddedKeyPC())
{
// Add any FKs for the fields of the (embedded) key
EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping)keyMapping;
for (int i=0;i<embMapping.getNumberOfJavaTypeMappings();i++)
{
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
if (ClassUtils.isReferenceType(embFmd.getType()) &&
embFieldMapping instanceof ReferenceMapping)
{
// Field is a reference type, so add a FK to the table of the PC for each PC implementation
Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd,
autoMode, storeMgr, clr);
foreignKeys.addAll(fks);
}
else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null &&
embFieldMapping.getNumberOfDatastoreMappings() > 0 &&
embFieldMapping 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(embFieldMapping, embFmd,
autoMode, storeMgr, clr);
if (fk != null)
{
foreignKeys.add(fk);
}
}
}
}
else if (mmd.getMap().keyIsPersistent())
{
// FK from join table to key table
referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getKeyType(), clr);
if (referencedTable != null)
{
// Take <foreign-key> from <key>
ForeignKeyMetaData fkmd = null;
if (mmd.getKeyMetaData() != null)
{
fkmd = mmd.getKeyMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode)
{
ForeignKey fk = new ForeignKey(keyMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
}
}