@Override
public void process() {
super.process();
// Create a M-M mapping and process common collection mapping metadata.
ManyToManyMapping mapping = new ManyToManyMapping();
process(mapping);
if (getMappedBy() == null || getMappedBy().equals("")) {
// Processing the owning side of a M-M that is process a join table.
processJoinTable(mapping);
} else {
// We are processing the a non-owning side of a M-M. Must set the
// mapping read-only.
mapping.setIsReadOnly(true);
// Get the owning mapping from the reference descriptor metadata.
ManyToManyMapping ownerMapping = null;
if (getOwningMapping(getMappedBy()).isManyToManyMapping()){
ownerMapping = (ManyToManyMapping)getOwningMapping(getMappedBy());
} else {
// If improper mapping encountered, throw an exception.
throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
}
// Set the relation table name from the owner.
mapping.setRelationTable(ownerMapping.getRelationTable());
// In a table per class inheritance we need to update the target
// keys before setting them to mapping's source key fields.
if (getDescriptor().usesTablePerClassInheritanceStrategy()) {
// Update the target key fields.
Vector<DatabaseField> targetKeyFields = new Vector<DatabaseField>();
for (DatabaseField targetKeyField : ownerMapping.getTargetKeyFields()) {
DatabaseField newTargetKeyField = (DatabaseField) targetKeyField.clone();
newTargetKeyField.setTable(getDescriptor().getPrimaryTable());
targetKeyFields.add(newTargetKeyField);
}
mapping.setSourceKeyFields(targetKeyFields);
// Update the targetRelationKeyFields.
Vector<DatabaseField> targetRelationKeyFields = new Vector<DatabaseField>();
for (DatabaseField targetRelationKeyField : ownerMapping.getTargetRelationKeyFields()) {
DatabaseField newTargetRelationKeyField = (DatabaseField) targetRelationKeyField.clone();
newTargetRelationKeyField.setTable(getDescriptor().getPrimaryTable());
targetRelationKeyFields.add(newTargetRelationKeyField);
}
mapping.setSourceRelationKeyFields(targetRelationKeyFields);
} else {
// Add all the source foreign keys we found on the owner.
mapping.setSourceKeyFields(ownerMapping.getTargetKeyFields());
mapping.setSourceRelationKeyFields(ownerMapping.getTargetRelationKeyFields());
}
// Add all the target foreign keys we found on the owner.
mapping.setTargetKeyFields(ownerMapping.getSourceKeyFields());
mapping.setTargetRelationKeyFields(ownerMapping.getSourceRelationKeyFields());
}
// Process properties
processProperties(mapping);