// 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.getNumberOfDatastoreFields();
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.getNumberOfDatastoreFields();
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
{
JPOXLogger.DATASTORE.warn(LOCALISER.msg("057041",
ownerfmd.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.
JPOXLogger.DATASTORE.warn(LOCALISER.msg("057012",
mfmd.getFullFieldName(), ownerfmd.getFullFieldName()));
}
}
}
}
else if (ownerfmd.getValueMetaData() != null && ownerfmd.getValueMetaData().getMappedBy() != null)
{
// Value field is stored in the key table
AbstractMemberMetaData vmd = null;
String value_field_name = ownerfmd.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 = getFieldMapping(map_field_name);
JavaTypeMapping valueMapping = getFieldMapping(vmd.getName());
if (dba.supportsNullsInCandidateKeys() || (!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.getNumberOfDatastoreFields();
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.getNumberOfDatastoreFields();
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
{
JPOXLogger.DATASTORE.warn(LOCALISER.msg("057042",
ownerfmd.getName()));