}
int fieldIndex = 1;
// bind the identity of the preparedStatement
if (identity.size() != ids.length) {
throw new PersistenceException(
"Size of identity field mismatched! expected: " + ids.length
+ " found: " + identity.size());
}
for (int i = 0; i < ids.length; i++) {
stmt.setObject(fieldIndex++, ids[i].toSQL(identity.get(i)));
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.format("jdo.loading", _type, stmt.toString()));
}
// execute the SQL query
rs = stmt.executeQuery();
if (!rs.next()) {
throw new ObjectNotFoundException(Messages.format(
"persist.objectNotFound", _type, identity));
}
// if this class is part of an extend relation (hierarchy), let's investigate
// what the real class type is vs. the specified one as part of the load statement;
// this is done by looking at (the id fields of all) the extending class
// desriptors, and by trying to find a (if not the) potential leaf descriptor;
// if there's no potential leaf descriptor, let's simply continue; if there's
// one, set the actual values in the ProposedEntity instance and return
// to indicate that the load shoul dbe re-tried with the correct ClassMolder
if (_extendingClassDescriptors.size() > 0) {
Object[] returnValues =
SQLHelper.calculateNumberOfFields(_extendingClassDescriptors,
ids.length, fields.length, _numberOfExtendLevels, rs);
ClassDescriptor potentialLeafDescriptor = (ClassDescriptor) returnValues[0];
if ((potentialLeafDescriptor != null)
&& !potentialLeafDescriptor.getJavaClass().getName().equals(_type)) {
entity.initializeFields(potentialLeafDescriptor.getFields().length);
entity.setActualEntityClass(potentialLeafDescriptor.getJavaClass());
entity.setExpanded(true);
}
// make sure that we only return early (as described above), if we actually
// found a potential leaf descriptor
if (potentialLeafDescriptor != null) {
return;
}
}
// Load all the fields of the object including one-one relations
// index to use during ResultSet.getXXX(); don't forget to ignore
// the identity columns
int columnIndex = ids.length + 1;
Set processedTables = new HashSet();
if (fields.length > 0 && fields[0].isJoined()) {
ClassDescriptor clsDesc = _engine.getDescriptor();
processedTables.add(new ClassDescriptorJDONature(clsDesc).getTableName());
}
boolean notNull;
// index in fields[] for storing result of SQLTypes.getObject()
fieldIndex = 1;
String tableName = null;
for (int i = 0; i < fields.length; ++i) {
SQLFieldInfo field = fields[i];
SQLColumnInfo[] columns = field.getColumnInfo();
tableName = field.getTableName();
if (i > 0 && !field.isJoined() && !processedTables.contains(tableName)) {
columnIndex = columnIndex + ids.length;
}
processedTables.add(tableName);
if (!field.isJoined() && (field.getJoinFields() == null)) {
entity.setField(columns[0].toJava(SQLTypeInfos.getValue(
rs, columnIndex++, columns[0].getSqlType())), i);
fieldIndex++;
} else if (!field.isMulti()) {
notNull = false;
Object[] id = new Object[columns.length];
for (int j = 0; j < columns.length; j++) {
id[j] = columns[j].toJava(SQLTypeInfos.getValue(
rs, columnIndex++, columns[j].getSqlType()));
fieldIndex++;
if (id[j] != null) { notNull = true; }
}
entity.setField(((notNull) ? new Identity(id) : null), i);
} else {
ArrayList res = new ArrayList();
notNull = false;
Object[] id = new Object[columns.length];
for (int j = 0; j < columns.length; j++) {
id[j] = columns[j].toJava(SQLTypeInfos.getValue(
rs, columnIndex, columns[j].getSqlType()));
if (id[j] != null) { notNull = true; }
fieldIndex++;
columnIndex++;
}
if (notNull) { res.add(new Identity(id)); }
entity.setField(res, i);
}
}
while (rs.next()) {
fieldIndex = 1;
columnIndex = ids.length + 1;
processedTables.clear();
if (fields[0].isJoined()) {
ClassDescriptor clsDesc = _engine.getDescriptor();
processedTables.add(new ClassDescriptorJDONature(clsDesc).getTableName());
}
for (int i = 0; i < fields.length; ++i) {
SQLFieldInfo field = fields[i];
SQLColumnInfo[] columns = field.getColumnInfo();
tableName = field.getTableName();
if (i > 0 && !field.isJoined() && !processedTables.contains(tableName)) {
columnIndex = columnIndex + ids.length;
}
processedTables.add(tableName);
if (field.isMulti()) {
ArrayList res = (ArrayList) entity.getField(i);
notNull = false;
Object[] id = new Object[columns.length];
for (int j = 0; j < columns.length; j++) {
id[j] = columns[j].toJava(SQLTypeInfos.getValue(
rs, columnIndex, columns[j].getSqlType()));
if (id[j] != null) { notNull = true; }
columnIndex++;
}
fieldIndex++;
if (notNull) {
Identity com = new Identity(id);
if (!res.contains(com)) { res.add(com); }
}
} else {
fieldIndex++;
columnIndex += columns.length;
}
}
}
} catch (SQLException except) {
LOG.fatal(Messages.format("jdo.loadFatal", _type,
(accessMode == AccessMode.DbLocked) ? _statementLock : _statementNoLock),
except);
throw new PersistenceException(Messages.format("persist.nested", except), except);
} finally {
JDOUtils.closeResultSet(rs);
JDOUtils.closeStatement(stmt);
}
}