fk.setName(table.getName() + "_" + fm.getName());
String[] fieldNames = fm.getSql().getName();
for (int i = 0; i < fieldNames.length; i++) {
for (int j = 0; j < table.getFieldCount(); j++) {
Field field = table.getField(j);
if (fieldNames[i].equals(field.getName())) { fk.addField(field); }
}
}
ClassMapping cm = _mappingHelper.getClassMappingByName(fm.getType());
if (cm == null) {
throw new GeneratorException("can not find class " + fm.getType());
}
String referenceTableName = cm.getMapTo().getTable();
Table referenceTable = null;
referenceTable = table.getSchema().getTable(referenceTableName);
fk.setReferenceTable(referenceTable);
String[] manykeys = fm.getSql().getManyKey();
if (manykeys == null || manykeys.length == 0) {
manykeys = _mappingHelper.getClassMappingSqlIdentity(cm, true);
}
for (int i = 0; i < manykeys.length; i++) {
for (int j = 0; j < referenceTable.getFieldCount(); j++) {
Field field = referenceTable.getField(j);
if (manykeys[i].equals(field.getName())) { fk.addReferenceField(field); }
}
}
fk.setRelationType(ForeignKey.ONE_ONE);
table.addForeignKey(fk);