You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Michael Schulze <mi...@t-systems.com> on 2003/05/16 11:21:06 UTC

retrieving references and performace

Hi all,

if i retrieve a collection of objects from the database with
getCollectionByQuery() and the objects have a reference (auto-retrive
true), OJB does a single select for every object in the collection to
retrieve the reference - which performs rather bad.

i managed to build an retrieveReference() on top of the PB that
retrieves a reference for a collection of objects in a single select.
isn't it possible for OJB to do the same with auto-retrieve ?    

    public void retrieveReference(Collection vehicles) {
        Map identityMap = new HashMap();
        for (Iterator it = vehicles.iterator(); it.hasNext();) {
            Vehicle value = (Vehicle) it.next();
            identityMap.put(value.getVehicleKey(), value);
            value.setVehicleEquipments(new LinkedList());
        }

        Criteria criteria = new Criteria();
        criteria.addIn("vehicleKey", identityMap.keySet());
        Query query = new QueryByCriteria(VehicleEquipment.class, criteria);
        Iterator equipments = broker.getIteratorByQuery(query);

        while (equipments.hasNext()) {
            VehicleEquipment equipment = (VehicleEquipment) equipments.next();
            Vehicle vehicle = (Vehicle) identityMap.get(equipment.getVehicleKey());
            vehicle.getVehicleEquipments().add(equipment);
        }
    }

Thanks,

Michael Schulze