protected 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 (isEmptyResult(res))
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 (isEmptyResult(res))
return false;
}
// figure out what type of object this is; the state manager
// only guarantees to provide a base class
Class<?> type;
if ((type = getType(res, mapping)) == null) {
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);
if (info != null && info.result != null) {
FieldMapping mappedByFieldMapping = info.result.
getMappedByFieldMapping();
Object mappedByObject = info.result.getMappedByValue();
if (mappedByFieldMapping != null && mappedByObject != null)
if (mappedByObject instanceof OpenJPAId &&
mapping.getExtraFieldDataIndex(mappedByFieldMapping.getIndex()) != -1) {
// The inverse relation can not be set since
// we are eagerly loading this sm for
// a sm owner that is still in the process of
// initializing itself.
// Remember owner oid by setIntermediate().
// The inverse relation is set later by
// setInverseRelation() when the sm owner is fully
// initialized.
int index = mappedByFieldMapping.getIndex();
if (sm.getLoaded().get(index)) {
sm.setImplData(index, mappedByObject);
} else {
sm.setIntermediate(index, mappedByObject);
}
} else {
setMappedBy(sm, mappedByFieldMapping, mappedByObject);
}
}
// 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);
getVersion(mapping, sm, res);
setInverseRelation(sm, mapping, res);
}
return true;
} finally {
if (res != null && (info == null || res != info.result))
res.close();
}
}