}
// For one-to-one, make sure that the field to recieve the FK
// is marked as the owning field
if (field instanceof OneToOne) {
OneToOne left = (OneToOne) field;
OneToOne right = (OneToOne) left.getRelatedField();
if (right != null) {
left.setMappedBy(null);
right.setMappedBy(left.getName());
}
}
EjbRelationshipRoleType.RoleMapping roleMapping = leftRole.getRoleMapping();
for (EjbRelationshipRoleType.RoleMapping.CmrFieldMapping cmrFieldMapping : roleMapping.getCmrFieldMapping()) {
JoinColumn joinColumn = new JoinColumn();
joinColumn.setName(cmrFieldMapping.getForeignKeyColumn());
joinColumn.setReferencedColumnName(cmrFieldMapping.getKeyColumn());
field.getJoinColumn().add(joinColumn);
}
} else {
JoinTable joinTable = new JoinTable();
joinTable.setName(relation.getManyToManyTableName());
//
// left
EjbRelationshipRoleType leftRole = roles.get(0);
RelationField left = null;
if (leftRole.getRelationshipRoleSource() != null) {
String leftEjbName = leftRole.getRelationshipRoleSource().getEjbName();
EntityData leftEntityData = entities.get(moduleId + "#" + leftEjbName);
if (leftEntityData == null) {
// todo warn no such entity in ejb-jar.xml
continue;
}
left = leftEntityData.relations.get(leftRole.getCmrField().getCmrFieldName());
}
if (left != null) {
left.setJoinTable(joinTable);
EjbRelationshipRoleType.RoleMapping roleMapping = leftRole.getRoleMapping();
for (EjbRelationshipRoleType.RoleMapping.CmrFieldMapping cmrFieldMapping : roleMapping.getCmrFieldMapping()) {
JoinColumn joinColumn = new JoinColumn();
joinColumn.setName(cmrFieldMapping.getForeignKeyColumn());
joinColumn.setReferencedColumnName(cmrFieldMapping.getKeyColumn());
joinTable.getJoinColumn().add(joinColumn);
}
}
//
// right
if (roles.size() > 1) {
EjbRelationshipRoleType rightRole = roles.get(1);
// if there wasn't a left cmr field, find the field for the right, so we can add the join table to it
if (left == null) {
RelationField right = left.getRelatedField();
if (right == null) {
if (rightRole.getCmrField() == null) {
// todo warn no cmr field declared for either role
continue;
}
if (rightRole.getRelationshipRoleSource() != null) {
String rightEjbName = rightRole.getRelationshipRoleSource().getEjbName();
EntityData rightEntityData = entities.get(moduleId + "#" + rightEjbName);
if (rightEntityData == null) {
// todo warn no such entity in ejb-jar.xml
continue;
}
right = rightEntityData.relations.get(rightRole.getCmrField().getCmrFieldName());
}
}
right.setJoinTable(joinTable);
}
EjbRelationshipRoleType.RoleMapping roleMapping = rightRole.getRoleMapping();
for (EjbRelationshipRoleType.RoleMapping.CmrFieldMapping cmrFieldMapping : roleMapping.getCmrFieldMapping()) {
JoinColumn joinColumn = new JoinColumn();