// these will be initailzed every time a new target entity
// is found in the result set (which should be ordered by table name among
// other things)
DbRelationship forwardRelationship = null;
DbRelationshipDetected reverseRelationship = null;
DbEntity fkEntity = null;
do {
short keySeq = rs.getShort("KEY_SEQ");
if (keySeq == 1) {
if (forwardRelationship != null) {
postprocessMasterDbRelationship(forwardRelationship);
forwardRelationship = null;
}
// start new entity
String fkEntityName = rs.getString("FKTABLE_NAME");
String fkName = rs.getString("FK_NAME");
if (!includeTableName(fkEntityName)) {
continue;
}
fkEntity = map.getDbEntity(fkEntityName);
if (fkEntity == null) {
logObj.info("FK warning: no entity found for name '"
+ fkEntityName
+ "'");
} else if (skippedEntities.contains(pkEntity) && skippedEntities.contains(fkEntity)) {
// cay-479 - don't load relationships between two
// skipped entities.
continue;
}
else {
// init relationship
forwardRelationship = new DbRelationship(DbLoader
.uniqueRelName(pkEntity, fkEntityName, true));
forwardRelationship.setSourceEntity(pkEntity);
forwardRelationship.setTargetEntity(fkEntity);
pkEntity.addRelationship(forwardRelationship);
reverseRelationship = new DbRelationshipDetected(uniqueRelName(
fkEntity,
pkEntName,
false));
reverseRelationship.setFkName(fkName);
reverseRelationship.setToMany(false);
reverseRelationship.setSourceEntity(fkEntity);
reverseRelationship.setTargetEntity(pkEntity);
fkEntity.addRelationship(reverseRelationship);
}
}
if (fkEntity != null) {
// Create and append joins
String pkName = rs.getString("PKCOLUMN_NAME");
String fkName = rs.getString("FKCOLUMN_NAME");
// skip invalid joins...
DbAttribute pkAtt = (DbAttribute) pkEntity.getAttribute(pkName);
if (pkAtt == null) {
logObj.info("no attribute for declared primary key: "
+ pkName);
continue;
}
DbAttribute fkAtt = (DbAttribute) fkEntity.getAttribute(fkName);
if (fkAtt == null) {
logObj.info("no attribute for declared foreign key: "
+ fkName);
continue;
}