// if this is ordered we need to know the next seq to use in case
// objects are added to the collection
if (field.getOrderColumn() != null) {
// we don't allow ordering table per class one-many's, so
// we know we don't need a union
Select sel = store.getSQLFactory().newSelect();
sel.setAggregate(true);
StringBuffer sql = new StringBuffer();
sql.append("MAX(").
append(sel.getColumnAlias(field.getOrderColumn())).
append(")");
sel.select(sql.toString(), field);
ClassMapping rel = getDefaultElementMapping(false);
sel.whereForeignKey(getJoinForeignKey(rel),
sm.getObjectId(), field.getDefiningMapping(), store);
Result res = sel.execute(store, fetch);
try {
res.next();
coll.getChangeTracker().setNextSequence
(res.getInt(field) + 1);
} finally {
res.close();
}
}
sm.storeObjectField(field.getIndex(), coll);
return;
}
//cache union for field here
// select data for this sm
boolean found = true;
final ClassMapping[] elems = getIndependentElementMappings(true);
final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
List parmList = null;
Union union = null;
SelectImpl sel = null;
Map<JDBCStoreManager.SelectKey, Object[]> storeCollectionUnionCache = null;
JDBCStoreManager.SelectKey selKey = null;
if (!((JDBCStoreManager)store).isQuerySQLCacheOn() || elems.length > 1)
union = newUnion(sm, store, fetch, elems, resJoins);
else {
parmList = new ArrayList();
JDBCFetchConfiguration fetchClone = new JDBCFetchConfigurationImpl();
fetchClone.copy(fetch);
// to specify the type so that no cast is needed
storeCollectionUnionCache = ((JDBCStoreManager)store).
getCacheMapFromQuerySQLCache(StoreCollectionFieldStrategy.class);
selKey =
new JDBCStoreManager.SelectKey(null, field, fetchClone);
Object[] objs = storeCollectionUnionCache.get(selKey);
if (objs != null) {
union = (Union) objs[0];
resJoins[0] = (Joins) objs[1];
}
else {
synchronized(storeCollectionUnionCache) {
objs = storeCollectionUnionCache.get(selKey);
if (objs == null) {
// select data for this sm
union = newUnion(sm, store, fetch, elems, resJoins);
found = false;
} else {
union = (Union) objs[0];
resJoins[0] = (Joins) objs[1];
}
sel = ((LogicalUnion.UnionSelect)union.getSelects()[0]).
getDelegate();
if (sel.getSQL() == null) {
((SelectImpl)sel).setSQL(store, fetch);
found = false;
}
// only cache the union when elems length is 1 for now
if (!found) {
Object[] objs1 = new Object[2];
objs1[0] = union;
objs1[1] = resJoins[0];
((JDBCStoreManager)store).addToSqlCache(
storeCollectionUnionCache, selKey, objs1);
}
}
}
Log log = store.getConfiguration().
getLog(JDBCConfiguration.LOG_JDBC);
if (log.isTraceEnabled()) {
if (found)
log.trace(_loc.get("cache-hit", field, this.getClass()));
else
log.trace(_loc.get("cache-missed", field, this.getClass()));
}
ClassMapping mapping = field.getDefiningMapping();
Object oid = sm.getObjectId();
Column[] cols = mapping.getPrimaryKeyColumns();
if (sel == null)
sel = ((LogicalUnion.UnionSelect)union.getSelects()[0]).
getDelegate();
sel.wherePrimaryKey(mapping, cols, cols, oid, store,
null, null, parmList);
List nonFKParams = sel.getSQL().getNonFKParameters();
if (nonFKParams != null && nonFKParams.size() > 0)
parmList.addAll(nonFKParams);
}
// create proxy