*/
private boolean initializeState(OpenJPAStateManager sm, PCState state,
JDBCFetchConfiguration fetch, ConnectionInfo info)
throws ClassNotFoundException, SQLException {
Object oid = sm.getObjectId();
ClassMapping mapping = (ClassMapping) sm.getMetaData();
Result res = null;
try {
if (info != null && info.result != null) {
res = info.result;
info.sm = sm;
if (info.mapping == null)
info.mapping = mapping;
mapping = info.mapping;
} else if (oid instanceof OpenJPAId
&& !((OpenJPAId) oid).hasSubclasses()) {
Boolean custom = customLoad(sm, mapping, state, fetch);
if (custom != null)
return custom.booleanValue();
res = getInitializeStateResult(sm, mapping, fetch,
Select.SUBS_EXACT);
if (res == null && !selectPrimaryKey(sm, mapping, fetch))
return false;
if (res != null && !res.next())
return false;
} else {
ClassMapping[] mappings = mapping.
getIndependentAssignableMappings();
if (mappings.length == 1) {
mapping = mappings[0];
Boolean custom = customLoad(sm, mapping, state, fetch);
if (custom != null)
return custom.booleanValue();
res = getInitializeStateResult(sm, mapping, fetch,
Select.SUBS_ANY_JOINABLE);
if (res == null && !selectPrimaryKey(sm, mapping, fetch))
return false;
} else
res = getInitializeStateUnionResult(sm, mapping, mappings,
fetch);
if (res != null && !res.next())
return false;
}
// figure out what type of object this is; the state manager
// only guarantees to provide a base class
Class type;
if (res == null)
type = mapping.getDescribedType();
else {
if (res.getBaseMapping() != null)
mapping = res.getBaseMapping();
res.startDataRequest(mapping.getDiscriminator());
try {
type = mapping.getDiscriminator().getClass(this, mapping,
res);
} finally {
res.endDataRequest();
}
}
// initialize the state manager; this may change the mapping
// and the object id instance if the type as determined
// from the indicator is a subclass of expected type
sm.initialize(type, state);
// load the selected mappings into the given state manager
if (res != null) {
// re-get the mapping in case the instance was a subclass
mapping = (ClassMapping) sm.getMetaData();
load(mapping, sm, fetch, res);
mapping.getVersion().afterLoad(sm, this);
}
return true;
} finally {
if (res != null && (info == null || res != info.result))
res.close();