* Integer[] {10,20,30} is not equal Long[] {10,20,30}
*/
protected void associateBatched(Collection owners, Collection children, Collection mToNImplementors)
{
CollectionDescriptor cds = getCollectionDescriptor();
PersistentField field = cds.getPersistentField();
PersistenceBroker pb = getBroker();
Class ownerTopLevelClass = pb.getTopLevelClass(getOwnerClassDescriptor().getClassOfObject());
Class childTopLevelClass = pb.getTopLevelClass(getItemClassDescriptor().getClassOfObject());
Class collectionClass = cds.getCollectionClass(); // this collection type will be used:
HashMap childMap = new HashMap();
HashMap ownerIdsToLists = new HashMap();
// initialize the owner list map
for (Iterator it = owners.iterator(); it.hasNext();)
{
Object owner = it.next();
ownerIdsToLists.put(new Identity(owner, pb), new ArrayList());
}
// build the children map
for (Iterator it = children.iterator(); it.hasNext();)
{
Object child = it.next();
childMap.put(new Identity(child, pb), child);
}
int ownerPkLen = getOwnerClassDescriptor().getPkFields().length;
int childPkLen = getItemClassDescriptor().getPkFields().length;
Object[] ownerPk = new Object[ownerPkLen];
Object[] childPk = new Object[childPkLen];
// build list of children based on m:n implementors
for (Iterator it = mToNImplementors.iterator(); it.hasNext();)
{
Object[] mToN = (Object[]) it.next();
System.arraycopy(mToN, 0, ownerPk, 0, ownerPkLen);
System.arraycopy(mToN, ownerPkLen, childPk, 0, childPkLen);
Identity ownerId = new Identity(null, ownerTopLevelClass, ownerPk);
Identity childId = new Identity(null, childTopLevelClass, childPk);
// Identities may not be equal due to type-mismatch
Collection list = (Collection) ownerIdsToLists.get(ownerId);
Object child = childMap.get(childId);
list.add(child);
}
// connect children list to owners
for (Iterator it = owners.iterator(); it.hasNext();)
{
Object result;
Object owner = it.next();
Identity ownerId = new Identity(owner, pb);
List list = (List) ownerIdsToLists.get(ownerId);
if ((collectionClass == null) && field.getType().isArray())
{
int length = list.size();
Class itemtype = field.getType().getComponentType();
result = Array.newInstance(itemtype, length);
for (int j = 0; j < length; j++)
{
Array.set(result, j, list.get(j));
}
}
else
{
ManageableCollection col = createCollection(collectionClass);
for (Iterator it2 = list.iterator(); it2.hasNext();)
{
col.ojbAdd(it2.next());
}
result = col;
}
Object value = field.get(owner);
if ((value instanceof CollectionProxyDefaultImpl) && (result instanceof Collection))
{
((CollectionProxyDefaultImpl) value).setData((Collection) result);
}
else
{
field.set(owner, result);
}
}
}