{
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
IndexMetaData imd = embFieldMapping.getMemberMetaData().getIndexMetaData();
if (imd != null)
{
Index index = TableUtils.getIndexForField(this, imd, embFieldMapping);
if (index != null)
{
indices.add(index);
}
}
}
}
else if (fieldMapping instanceof SerialisedMapping)
{
// Don't index these
}
else
{
// Add any required index for this field
IndexMetaData imd = fmd.getIndexMetaData();
if (imd != null)
{
// Index defined so add it
Index index = TableUtils.getIndexForField(this, imd, fieldMapping);
if (index != null)
{
indices.add(index);
}
}
else if (autoMode)
{
if (fmd.getIndexed() == null)
{
// Indexing not set, so add where we think it is appropriate
if (!fmd.isPrimaryKey()) // Ignore PKs since they will be indexed anyway
{
int relationType = fmd.getRelationType(clr);
if (relationType == Relation.ONE_TO_ONE_UNI)
{
// 1-1 with FK at this side so index the FK
if (fieldMapping instanceof ReferenceMapping)
{
ReferenceMapping refMapping = (ReferenceMapping)fieldMapping;
if (refMapping.getMappingStrategy() == ReferenceMapping.PER_IMPLEMENTATION_MAPPING)
{
// Cols per implementation : index each of implementations
if (refMapping.getJavaTypeMapping() != null)
{
int colNum = 0;
JavaTypeMapping[] implMappings = refMapping.getJavaTypeMapping();
for (int i=0;i<implMappings.length;i++)
{
int numColsInImpl = implMappings[i].getNumberOfDatastoreMappings();
Index index = new Index(this, false, null);
for (int j=0;j<numColsInImpl;j++)
{
index.setColumn(j,
(Column)fieldMapping.getDatastoreMapping(colNum++).getDatastoreField());
}
indices.add(index);
}
}
}
}
else
{
Index index = new Index(this, false, null);
for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
{
index.setColumn(i, (Column)fieldMapping.getDatastoreMapping(i).getDatastoreField());
}
indices.add(index);
}
}
else if (relationType == Relation.ONE_TO_ONE_BI && fmd.getMappedBy() == null)
{
// 1-1 with FK at this side so index the FK
Index index = new Index(this, false, null);
for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
{
index.setColumn(i, (Column)fieldMapping.getDatastoreMapping(i).getDatastoreField());
}
indices.add(index);
}
else if (relationType == Relation.MANY_TO_ONE_BI)
{
// N-1 with FK at this side so index the FK
AbstractMemberMetaData relMmd = fmd.getRelatedMemberMetaData(clr)[0];
if (relMmd.getJoinMetaData() == null && fmd.getJoinMetaData() == null)
{
Index index = new Index(this, false, null);
for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
{
index.setColumn(i, (Column)fieldMapping.getDatastoreMapping(i).getDatastoreField());
}
indices.add(index);
}
}
}
}
}
}
}
// Check if any version column needs indexing
if (versionMapping != null)
{
IndexMetaData idxmd = getVersionMetaData().getIndexMetaData();
if (idxmd != null)
{
Index index = new Index(this, idxmd.isUnique(),
idxmd.getValueForExtension("extended-setting"));
if (idxmd.getName() != null)
{
index.setName(idxmd.getName());
}
int countVersionFields = versionMapping.getNumberOfDatastoreMappings();
for (int i=0; i<countVersionFields; i++)
{
index.addDatastoreField(versionMapping.getDatastoreMapping(i).getDatastoreField());
}
indices.add(index);
}
}
// Check if any discriminator column needs indexing
if (discriminatorMapping != null)
{
DiscriminatorMetaData dismd = getDiscriminatorMetaData();
IndexMetaData idxmd = dismd.getIndexMetaData();
if (idxmd != null)
{
Index index = new Index(this, idxmd.isUnique(),
idxmd.getValueForExtension("extended-setting"));
if (idxmd.getName() != null)
{
index.setName(idxmd.getName());
}
int countDiscrimFields = discriminatorMapping.getNumberOfDatastoreMappings();
for (int i=0; i<countDiscrimFields; i++)
{
index.addDatastoreField(discriminatorMapping.getDatastoreMapping(i).getDatastoreField());
}
indices.add(index);
}
}
// Add on any order fields (for lists, arrays, collections) that need indexing
Set orderMappingsEntries = getExternalOrderMappings().entrySet();
Iterator orderMappingsEntriesIter = orderMappingsEntries.iterator();
while (orderMappingsEntriesIter.hasNext())
{
Map.Entry entry = (Map.Entry)orderMappingsEntriesIter.next();
AbstractMemberMetaData fmd = (AbstractMemberMetaData)entry.getKey();
JavaTypeMapping mapping = (JavaTypeMapping)entry.getValue();
OrderMetaData omd = fmd.getOrderMetaData();
if (omd != null && omd.getIndexMetaData() != null)
{
Index index = getIndexForIndexMetaDataAndMapping(omd.getIndexMetaData(), mapping);
if (index != null)
{
indices.add(index);
}
}
}
// Add on any user-required indices for the class(es) as a whole (subelement of <class>)
Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
while (cmdIter.hasNext())
{
AbstractClassMetaData thisCmd = cmdIter.next();
IndexMetaData[] classIndices = thisCmd.getIndexMetaData();
if (classIndices != null)
{
for (int i=0;i<classIndices.length;i++)
{
Index index = getIndexForIndexMetaData(classIndices[i]);
if (index != null)
{
indices.add(index);
}
}