}
}
protected Object value(final Object obj) {
final OpenJPAStateManager sm = assertOwner();
final JDBCStore store = getStore();
if (_strat.getFieldMapping().getElement().getValueMappedBy() != null) {
Object val = _strat.deriveValue(store, obj);
if (hasValue(val))
return val;
return null;
}
final JDBCFetchConfiguration fetch = store.getFetchConfiguration();
final ClassMapping[] clss = _strat.getIndependentValueMappings(true);
final Joins[] resJoins = new Joins[Math.max(1, clss.length)];
Union union = store.getSQLFactory().newUnion(Math.max(1, clss.length));
union.setExpectedResultCount(1, false);
if (fetch.getSubclassFetchMode(_strat.getFieldMapping().
getElementMapping().getTypeMapping())
!= JDBCFetchConfiguration.EAGER_JOIN)
union.abortUnion();
union.select(new Union.Selector() {
public void select(Select sel, int idx) {
ClassMapping cls = (clss.length == 0) ? null : clss[idx];
sel.whereForeignKey(_strat.getJoinForeignKey(cls),
sm.getObjectId(), _strat.getFieldMapping().
getDefiningMapping(), store);
if (_strat.getFieldMapping().getKey().getValueMappedBy()
!= null)
resJoins[idx] = _strat.joinValueRelation(sel.newJoins(),
cls);
Object key = _strat.toKeyDataStoreValue(obj, store);
Column[] cols = _strat.getKeyColumns(cls);
Object[] vals = (cols.length == 1) ? null : (Object[]) key;
SQLBuffer sql = new SQLBuffer(store.getDBDictionary());
for (int i = 0; i < cols.length; i++) {
if (i > 0)
sql.append(" AND ");
sql.append(sel.getColumnAlias(cols[i], resJoins[idx]));
if (vals == null)
sql.append((key == null) ? " IS " : " = ").
appendValue(key, cols[i]);
else
sql.append((vals[i] == null) ? " IS " : " = ").
appendValue(vals[i], cols[i]);
}
sel.where(sql, resJoins[idx]);
if (resJoins[idx] == null)
resJoins[idx] = _strat.joinValueRelation(sel.newJoins(),
cls);
_strat.selectValue(sel, cls, sm, store, fetch, resJoins[idx]);
}
});
Result res = null;
try {
res = union.execute(store, fetch);
if (res.next())
return _strat.loadValue(sm, store, fetch, res,
resJoins[res.indexOf()]);
return null;
} catch (SQLException se) {
throw SQLExceptions.getStore(se, store.getDBDictionary());
} finally {
if (res != null)
res.close();
}
}