// a unique constraint on them. If the key field is in a superclass then we
// cannot do this so just omit it.
if (keyMapping.getDatastoreContainer() == this &&
ownerMapping.getDatastoreContainer() == this)
{
CandidateKey ck = new CandidateKey(this);
// This HashSet is to avoid duplicate adding of columns.
HashSet addedColumns = new HashSet();
// Add columns for the owner field
int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
for (int i = 0; i < countOwnerFields; i++)
{
Column col = (Column) ownerMapping.getDatastoreMapping(i).getDatastoreField();
addedColumns.add(col);
ck.addDatastoreField(col);
}
// Add columns for the key field
int countKeyFields = keyMapping.getNumberOfDatastoreMappings();
for (int i = 0; i < countKeyFields; i++)
{
Column col = (Column) keyMapping.getDatastoreMapping(i).getDatastoreField();
if (!addedColumns.contains(col))
{
addedColumns.add(col);
ck.addDatastoreField(col);
}
else
{
NucleusLogger.DATASTORE.warn(LOCALISER.msg("057041",
ownerMmd.getName()));
}
}
if (candidateKeysByMapField.put(mfmd, ck) != null)
{
// We have multiple "mapped-by" coming to this field so give a warning that this may potentially
// cause problems. For example if they have the key field defined here for 2 different relations
// so you may get keys/values appearing in the other relation that shouldn't be.
// Logged as a WARNING for now.
// If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
NucleusLogger.DATASTORE.warn(LOCALISER.msg("057012",
mfmd.getFullFieldName(), ownerMmd.getFullFieldName()));
}
}
}
}
else if (ownerMmd.getValueMetaData() != null && ownerMmd.getValueMetaData().getMappedBy() != null)
{
// Value field is stored in the key table
AbstractMemberMetaData vmd = null;
String value_field_name = ownerMmd.getValueMetaData().getMappedBy();
if (value_field_name != null)
{
vmd = cmd.getMetaDataForMember(value_field_name);
}
if (vmd == null)
{
throw new ClassDefinitionException(LOCALISER.msg("057008", mfmd));
}
JavaTypeMapping ownerMapping = getMemberMapping(map_field_name);
JavaTypeMapping valueMapping = getMemberMapping(vmd.getName());
if (dba.supportsOption(RDBMSAdapter.NULLS_IN_CANDIDATE_KEYS) ||
(!ownerMapping.isNullable() && !valueMapping.isNullable()))
{
// If the owner and value fields are represented in this table then we can impose
// a unique constraint on them. If the value field is in a superclass then we
// cannot do this so just omit it.
if (valueMapping.getDatastoreContainer() == this &&
ownerMapping.getDatastoreContainer() == this)
{
CandidateKey ck = new CandidateKey(this);
// This HashSet is to avoid duplicate adding of columns.
HashSet addedColumns = new HashSet();
// Add columns for the owner field
int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
for (int i = 0; i < countOwnerFields; i++)
{
Column col = (Column) ownerMapping.getDatastoreMapping(i).getDatastoreField();
addedColumns.add(col);
ck.addDatastoreField(col);
}
// Add columns for the value field
int countValueFields = valueMapping.getNumberOfDatastoreMappings();
for (int i = 0; i < countValueFields; i++)
{
Column col = (Column) valueMapping.getDatastoreMapping(i).getDatastoreField();
if (!addedColumns.contains(col))
{
addedColumns.add(col);
ck.addDatastoreField(col);
}
else
{
NucleusLogger.DATASTORE.warn(LOCALISER.msg("057042",
ownerMmd.getName()));