final Joins[] resJoins = new Joins[rels.length];
// select related mapping columns; joining from the related type
// back to our fk table if not an inverse mapping (in which case we
// can just make sure the inverse cols == our pk values)
Union union = store.getSQLFactory().newUnion(rels.length);
union.setExpectedResultCount(1, false);
if (fetch.getSubclassFetchMode(field.getTypeMapping())
!= JDBCFetchConfiguration.EAGER_JOIN)
union.abortUnion();
union.select(new Union.Selector() {
public void select(Select sel, int idx) {
if (field.getJoinDirection() == field.JOIN_INVERSE)
sel.whereForeignKey(field.getForeignKey(rels[idx]),
sm.getObjectId(), field.getDefiningMapping(), store);
else {
if (!field.isBiMTo1JT()) {
resJoins[idx] = sel.newJoins().joinRelation(field.getName(),
field.getForeignKey(rels[idx]), rels[idx],
field.getSelectSubclasses(), false, false);
field.wherePrimaryKey(sel, sm, store);
} else {
resJoins[idx] = sel.newJoins().joinRelation(null,
field.getBi1ToMJoinFK(), rels[idx],
field.getSelectSubclasses(), false, false);
sel.whereForeignKey(field.getBi1ToMElemFK(), sm.getObjectId(),
field.getDefiningMapping(), store);
}
}
sel.select(rels[idx], subs, store, fetch, fetch.EAGER_JOIN,
resJoins[idx]);
}
});
Result res = union.execute(store, fetch);
try {
Object val = null;
if (res.next())
val = res.load(rels[res.indexOf()], store, fetch,
resJoins[res.indexOf()]);