mappingDefinition = stmtMapping;
mappedFieldNumbers = fieldNumbers;
}
// Extract any surrogate version
VersionMetaData vermd = cmd.getVersionMetaDataForClass();
Object surrogateVersion = null;
StatementMappingIndex versionMapping = null;
if (vermd != null)
{
if (vermd.getFieldName() == null)
{
versionMapping = stmtMapping.getMappingForMemberPosition(StatementClassMapping.MEMBER_VERSION);
}
else
{
AbstractMemberMetaData vermmd = cmd.getMetaDataForMember(vermd.getFieldName());
versionMapping = stmtMapping.getMappingForMemberPosition(vermmd.getAbsoluteFieldNumber());
}
}
if (versionMapping != null)
{
// Surrogate version column returned by query
JavaTypeMapping mapping = versionMapping.getMapping();
surrogateVersion = mapping.getObject(ec, rs, versionMapping.getColumnPositions());
}
// Extract the object from the ResultSet
Object obj = null;
boolean needToSetVersion = false;
if (persistentClass.isInterface() && !cmd.isImplementationOfPersistentDefinition())
{
// Querying by interface, and not a generated implementation so use the metadata for the interface
cmd = ec.getMetaDataManager().getMetaDataForInterface(persistentClass, clr);
}
if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
// Check if the PK field(s) are all null (implies null object, when using OUTER JOIN)
int[] pkNumbers = cmd.getPKMemberPositions();
ResultSet resultSet = (ResultSet)rs;
boolean nullObject = true;
for (int i=0;i<pkNumbers.length;i++)
{
StatementMappingIndex pkIdx = mappingDefinition.getMappingForMemberPosition(pkNumbers[i]);
if (pkIdx == null)
{
throw new NucleusException("You have just executed an SQL statement yet the information " +
"for the primary key column(s) is not available! " +
"Please generate a testcase and report this issue");
}
int[] colPositions = pkIdx.getColumnPositions();
for (int j=0;j<colPositions.length;j++)
{
try
{
Object pkObj = resultSet.getObject(colPositions[j]);
if (pkObj != null)
{
nullObject = false;
break;
}
}
catch (SQLException sqle)
{
NucleusLogger.DATASTORE_RETRIEVE.warn("Exception thrown while retrieving results ", sqle);
}
if (!nullObject)
{
break;
}
}
}
if (nullObject)
{
obj = null;
}
else
{
// Retrieve the object with this application-identity
if (warnMsg != null)
{
NucleusLogger.DATASTORE_RETRIEVE.warn(warnMsg);
}
obj = getObjectForApplicationId(ec, rs, mappingDefinition, mappedFieldNumbers,
pcClassForObject, cmd, requiresInheritanceCheck, surrogateVersion);
}
}
else if (cmd.getIdentityType() == IdentityType.DATASTORE)
{
// Generate the "oid" for this object (of type pcClassForObject), and find the object for that
StatementMappingIndex datastoreIdMapping =
stmtMapping.getMappingForMemberPosition(StatementClassMapping.MEMBER_DATASTORE_ID);
JavaTypeMapping mapping = datastoreIdMapping.getMapping();
OID oid = (OID)mapping.getObject(ec, rs, datastoreIdMapping.getColumnPositions());
if (oid != null)
{
Object id = oid;
if (!pcClassForObject.getName().equals(oid.getPcClass()))
{
// Get an OID for the right inheritance level
id = OIDFactory.getInstance(ec.getNucleusContext(), pcClassForObject.getName(), oid.getKeyValue());
}
if (warnMsg != null)
{
NucleusLogger.DATASTORE_RETRIEVE.warn(warnMsg);
}
if (mappedFieldNumbers == null)
{
obj = ec.findObject(id, false, requiresInheritanceCheck, null);
needToSetVersion = true;
}
else
{
obj = getObjectForDatastoreId(ec, rs, mappingDefinition, mappedFieldNumbers,
id, requiresInheritanceCheck ? null : pcClassForObject, cmd, surrogateVersion);
}
}
else
{
obj = null;
}
}
else if (cmd.getIdentityType() == IdentityType.NONDURABLE)
{
Object id = ec.newObjectId(className, null);
if (mappedFieldNumbers == null)
{
obj = ec.findObject(id, false, requiresInheritanceCheck, null);
needToSetVersion = true;
}
else
{
obj = getObjectForDatastoreId(ec, rs, mappingDefinition, mappedFieldNumbers,
id, pcClassForObject, cmd, surrogateVersion);
}
}
if (obj != null && needToSetVersion)
{
// Set the version of the object where possible
if (surrogateVersion != null)
{
ObjectProvider objSM = ec.findObjectProvider(obj);
objSM.setVersion(surrogateVersion);
}
else
{
if (vermd != null && vermd.getFieldName() != null)
{
// Version stored in a normal field
int versionFieldNumber = acmd.getMetaDataForMember(vermd.getFieldName()).getAbsoluteFieldNumber();
if (stmtMapping.getMappingForMemberPosition(versionFieldNumber) != null)
{
ObjectProvider objSM = ec.findObjectProvider(obj);
Object verFieldValue = objSM.provideField(versionFieldNumber);
if (verFieldValue != null)