if (joinColumns.isEmpty()) {
if (descriptor.hasCompositePrimaryKey()) {
// Add a default one for each part of the composite primary
// key. Foreign and primary key to have the same name.
for (DatabaseField primaryKeyField : descriptor.getPrimaryKeyFields()) {
JoinColumnMetadata joinColumn = new JoinColumnMetadata();
joinColumn.setReferencedColumnName(primaryKeyField.getName());
joinColumn.setName(primaryKeyField.getName());
joinColumns.add(joinColumn);
}
} else {
// Add a default one for the single case, not setting any
// foreign and primary key names. They will default based
// on which accessor is using them.
joinColumns.add(new JoinColumnMetadata());
}
} else {
// Need to update any join columns that use a foreign key name
// for the primary key name. E.G. User specifies the renamed id
// field name from a primary key join column as the primary key in
// an inheritance subclass.
for (JoinColumnMetadata joinColumn : joinColumns) {
// Doing this could potentially change a value entered in XML.
// However, in this case I think that is ok since in theory we
// are writing out the correct value that EclipseLink needs to
// form valid queries.
String referencedColumnName = joinColumn.getReferencedColumnName();
// The referenced column name in a variable one to one case is a
// query key name and not a column name so bypass any of this
// code.
if (referencedColumnName != null && !isVariableOneToOne()) {
DatabaseField referencedField = new DatabaseField(referencedColumnName);
joinColumn.setReferencedColumnName(descriptor.getPrimaryKeyJoinColumnAssociation(referencedField).getName());
}
}
}
if (descriptor.hasCompositePrimaryKey()) {
// The number of join columns should equal the number of primary key fields.
if (joinColumns.size() != descriptor.getPrimaryKeyFields().size()) {
throw ValidationException.incompleteJoinColumnsSpecified(getAnnotatedElement(), getJavaClass());
}
// All the primary and foreign key field names should be specified.
for (JoinColumnMetadata joinColumn : joinColumns) {
if (joinColumn.isPrimaryKeyFieldNotSpecified() || joinColumn.isForeignKeyFieldNotSpecified()) {
throw ValidationException.incompleteJoinColumnsSpecified(getAnnotatedElement(), getJavaClass());
}
}
}