{
ClassLoaderResolver clr = sm.getObjectManager().getClassLoaderResolver();
// TODO Allow for more than just the first elementTable (currently take the columns to select from the first element table only)
if (elementsAreEmbedded || elementsAreSerialised)
{
return new PersistentIDROF(null , null, emd, null, null, null, ignoreCache, false,
stmt.hasMetaDataExpression(), null, clr.classForName(elementType));
}
else if (elementMapping instanceof ReferenceMapping)
{
// Element = Reference (Interface/Object)
// TODO Respect any fetch plan for the implementation class
// This selects the id column(s) in the join table of all implementations of the reference type
// It is processed by ReferenceMapping where it decides which of the implementation types is stored in this row.
JavaTypeMapping[] implMappings = ((ReferenceMapping)elementMapping).getJavaTypeMapping();
for (int i=0;i<implMappings.length;i++)
{
stmt.select(implMappings[i]);
}
return new PersistentIDROF(elementInfo != null ? elementInfo[0].getDatastoreClass() : null, null, emd,
null, null, null, ignoreCache, false,
stmt.hasMetaDataExpression(), null, clr.classForName(elementType));
}
else
{
// Element = PC
int[] datastoreIndex = null;
int[] versionIndex = null;
// Select any datastore/version columns
if (stmt.getTableExpression(elmIdentifier) != null)
{
if (elementInfo[0].getDatastoreClass().getIdentityType() == IdentityType.DATASTORE)
{
datastoreIndex = stmt.select(elmIdentifier, elementInfo[0].getDatastoreClass().getDataStoreObjectIdMapping(), true);
}
JavaTypeMapping verMapping = elementInfo[0].getDatastoreClass().getVersionMapping(true);
if (verMapping != null)
{
versionIndex = stmt.select(elmIdentifier, verMapping, true);
}
}
else
{
if (elementInfo[0].getDatastoreClass().getIdentityType() == IdentityType.DATASTORE)
{
datastoreIndex = stmt.select(stmt.getMainTableAlias(),
elementInfo[0].getDatastoreClass().getDataStoreObjectIdMapping(),true);
}
JavaTypeMapping verMapping = elementInfo[0].getDatastoreClass().getVersionMapping(true);
if (verMapping != null)
{
versionIndex = stmt.select(stmt.getMainTableAlias(), verMapping, true);
}
}
StatementExpressionIndex[] statementExpressionIndex = null;
int[] prefetchFieldNumbers = null;
if (useFetchPlan)
{
// Select the FetchPlan fields
FetchPlan fp = sm.getObjectManager().getFetchPlan();
fp.manageFetchPlanForClass(emd);
FetchPlanForClass fpc = fp.getFetchPlanForClass(emd);
int fieldNumbers[] = fpc.getFieldsInActualFetchPlan();
int fn[] = new int[fieldNumbers.length];
int prefetchFieldCount = 0;
int fieldCount = emd.getNoOfInheritedManagedMembers() + emd.getNoOfManagedMembers();
statementExpressionIndex = new StatementExpressionIndex[fieldCount];
for (int i=0; i<fieldNumbers.length; ++i)
{
AbstractMemberMetaData fmd = emd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumbers[i]);
if (fmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
{
JavaTypeMapping m = elementInfo[0].getDatastoreClass().getFieldMapping(fmd);
if (m != null) // Field is not a Collection, List, Map, etc so has a datastore mapping
{
if (m.includeInFetchStatement() && !(m instanceof AbstractContainerMapping))
{
statementExpressionIndex[fieldNumbers[i]] = new StatementExpressionIndex();
statementExpressionIndex[fieldNumbers[i]].setMapping(m);
fn[prefetchFieldCount++] = fieldNumbers[i];
}
}
}
}
prefetchFieldNumbers = new int[prefetchFieldCount];
System.arraycopy(fn, 0, prefetchFieldNumbers, 0, prefetchFieldCount);
}
else
{
// if no fetchplan is used and is application identity, select only PK fields
if (emd.getIdentityType() == IdentityType.APPLICATION)
{
prefetchFieldNumbers = new int[emd.getPKMemberPositions().length];
int fieldCount = emd.getNoOfInheritedManagedMembers() + emd.getNoOfManagedMembers();
statementExpressionIndex = new StatementExpressionIndex[fieldCount];
for (int i = 0; i < prefetchFieldNumbers.length; ++i)
{
prefetchFieldNumbers[i] = emd.getPKMemberPositions()[i];
AbstractMemberMetaData fmd = emd.getMetaDataForManagedMemberAtAbsolutePosition(prefetchFieldNumbers[i]);
if (fmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
{
JavaTypeMapping m = elementInfo[0].getDatastoreClass().getFieldMapping(fmd);
if (m != null) // Field is not a Collection, List, Map, etc so has a datastore mapping
{
if (m.includeInFetchStatement() && !(m instanceof AbstractContainerMapping))
{
statementExpressionIndex[prefetchFieldNumbers[i]] = new StatementExpressionIndex();
statementExpressionIndex[prefetchFieldNumbers[i]].setMapping(m);
}
}
}
}
}
}
if (stmt.getTableExpression(elmIdentifier) != null)
{
Mappings.selectMapping(stmt, elmIdentifier, statementExpressionIndex);
}
else
{
Mappings.selectMapping(stmt, statementExpressionIndex);
}
return new PersistentIDROF(elementInfo != null ? elementInfo[0].getDatastoreClass() : null,
prefetchFieldNumbers, emd, statementExpressionIndex, datastoreIndex, versionIndex,
ignoreCache, iterateUsingDiscriminator,
stmt.hasMetaDataExpression(), null, clr.classForName(elementType));
}
}