// Fields are stored in a field-keyed map first before adding them
// to the builder.
// This ensures the fields from foreign keys with multiple columns
// will only get created once.
final FieldMetadataBuilder fieldBuilder = getOneToOneOrManyToOneField(
fieldName, fieldType, foreignKey, ONE_TO_ONE, false);
uniqueFields.put(fieldName, fieldBuilder);
}
for (final FieldMetadataBuilder fieldBuilder : uniqueFields.values()) {
addToBuilder(fieldBuilder);
// Exclude these fields in @RooToString to avoid circular references
// - ROO-1399
excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName()
.getSymbolName());
}
// Add one-to-one mapped-by fields
if (table.isJoinTable()) {
return;
}
for (final ForeignKey exportedKey : table.getExportedKeys()) {
final Table exportedKeyForeignTable = exportedKey.getForeignTable();
Validate.notNull(
exportedKeyForeignTable,
"Foreign key table for foreign key '%s' in table '%s' does not exist. One-to-one relationship not created",
exportedKey.getName(), table.getFullyQualifiedTableName());
if (exportedKeyForeignTable.isJoinTable()) {
continue;
}
final String foreignTableName = exportedKeyForeignTable.getName();
final String foreignSchemaName = exportedKeyForeignTable
.getSchema().getName();
final Table foreignTable = database.getTable(foreignTableName,
foreignSchemaName);
Validate.notNull(
foreignTable,
"Related table '%s' could not be found but has a foreign-key reference to table '%s'",
exportedKeyForeignTable.getFullyQualifiedTableName(),
table.getFullyQualifiedTableName());
if (!isOneToOne(foreignTable,
foreignTable.getImportedKey(exportedKey.getName()))) {
continue;
}
final Short keySequence = exportedKey.getKeySequence();
final String fieldSuffix = keySequence != null && keySequence > 0 ? String
.valueOf(keySequence) : "";
JavaSymbolName fieldName = new JavaSymbolName(
DbreTypeUtils.suggestFieldName(foreignTableName)
+ fieldSuffix);
final JavaType fieldType = DbreTypeUtils.findTypeForTableName(
managedEntities, foreignTableName, foreignSchemaName);
Validate.notNull(
fieldType,
"Attempted to create one-to-one mapped-by field '%s' in '%s' %s",
fieldName, destination.getFullyQualifiedTypeName(),
getErrorMsg(foreignTable.getFullyQualifiedTableName()));
// Check for existence of same field - ROO-1691
while (true) {
if (!hasFieldInItd(fieldName)) {
break;
}
fieldName = new JavaSymbolName(fieldName.getSymbolName() + "_");
}
final JavaSymbolName mappedByFieldName = new JavaSymbolName(
DbreTypeUtils.suggestFieldName(table.getName())
+ fieldSuffix);
final FieldMetadataBuilder fieldBuilder = getOneToOneMappedByField(
fieldName, fieldType, mappedByFieldName,
exportedKey.getOnUpdate(), exportedKey.getOnDelete());
addToBuilder(fieldBuilder);
// Exclude these fields in @RooToString to avoid circular references
// - ROO-1399
excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName()
.getSymbolName());
}
}