* @param fm FieldMapping of relation.
* @throws GeneratorException If failed to create foreign key schema object.
*/
private void addOneOneForeignKey(final Table table, final FieldMapping fm)
throws GeneratorException {
ForeignKey fk = _schemaFactory.createForeignKey();
fk.setConfiguration(_configuration);
fk.setTable(table);
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);
}