String mapping = "";
// In belongs-to relation, class A holds FK and is also the owner.
if (Relation.BELONGS_TO_TYPE.equalsIgnoreCase(type)) {
ActiveRecord targetHome = (ActiveRecord)ActiveRecordUtil.getHomeInstance(b);
RowInfo ri = targetHome.getRowInfo();
if ( ri == null) {
throw new RelationException("The RowInfo for class " + b.getName() + " cannot be null.");
}
String[] pkNames = ri.getPrimaryKeyColumnNames();
if (StringUtil.isStringInArray("ID", pkNames, true)) {
String fk = target + "_id";
verifyExistenceOfColumn(a, fk);
mapping = fk + "=id";
}
else {
int size = pkNames.length;
for (int i=0; i<size-1; i++) {
String fk = pkNames[i];
verifyExistenceOfColumn(a, fk);
mapping += fk + "=" + fk + ",";
}
String fk = pkNames[size-1];
verifyExistenceOfColumn(a, fk);
mapping += fk + "=" + fk;
}
}
else
// In has-one and has-many relation, class B holds FK.
if (Relation.HAS_ONE_TYPE.equalsIgnoreCase(type) ||
Relation.HAS_MANY_TYPE.equalsIgnoreCase(type)) {
ActiveRecord ownerHome = (ActiveRecord)ActiveRecordUtil.getHomeInstance(a);
RowInfo ri = ownerHome.getRowInfo();
if ( ri == null) {
throw new RelationException("The RowInfo for class " + a.getName() + " cannot be null.");
}
String[] pkNames = ri.getPrimaryKeyColumnNames();
if (StringUtil.isStringInArray("ID", pkNames, true)) {
String fk = ActiveRecordUtil.getModelName(a) + "_id";
verifyExistenceOfColumn(b, fk);
mapping = "id=" + fk;
}