> This helper is meant for situations where reference- and
> collection-descriptors are set to auto-retrieve="false".
> And the user wants to load the references by an explicit API call.
>
> We have all necessary stuff in PBImpl. Thus I'm planning to simply
> expose two new methods in the PB interface:
>
> retrieveAllReferences(Object instance)
> retrieveReference(Object instance, String referenceAttribute)

It would also be reaaaally nice if one could just get the collection
returned directly without storing it inside the instance object! e.g.

Collection retrieveReferenceAsCollection(Object instance, String
referenceAttribute);

And even better would be to be able to somehow "extend" the query used to
retreive the references with some specific criteria. e.g.

retrieveReferenceAsCollection(Object instance, String referenceAttribute,
Criteria criteria)

so one could limit a possible 10000 return to those 7 tuples that are
interesting in the current context.

/max


> Jakob Braeuchi wrote:
> > hi,
> >
> > i have to admit i haven't always followed this thread so please forgive
my
> > ignorance.
> > what's the purpose of the relationship helper ?
> > could it be integrated into ojb ?
> >
> > jakob
> >
> > ----- Original Message -----
> > From: "Charles Anthony" <[EMAIL PROTECTED]>
> > To: "'OJB Users List'" <[EMAIL PROTECTED]>
> > Sent: Monday, September 09, 2002 10:45 AM
> > Subject: RE: Anyone up for a "challange" ? :)
> >
> >
> >
> >>>Willingly to share the code - just to see it ? :)
> >>>
> >>>/max
> >>
> >>Voila!
> >>
> >>My RelationshipHelper delegates to a more complex object, the
appropriate
> >>sections of which are detailed below.
> >>
> >>Cheers,
> >>
> >>Charles.
> >>
> >>    /**
> >>     * If the relationship specified is not null, load the the named
> >>     * relationship
> >>     * @param o instance of an object
> >>     * @param relationshipName the name of the relationship to load
> >>     */
> >>    public void loadRelationship(Object o, String relationshipName) {
> >>        PersistenceBroker broker =
> >>PersistenceBrokerFactory.defaultPersistenceBroker();
> >>
> >>        ClassDescriptor cld = broker.getClassDescriptor(o.getClass());
> >>        ObjectReferenceDescriptor rd =
> >>cld.getObjectReferenceDescriptorByName(relationshipName);
> >>        if (rd == null) {
> >>            rd = cld.getCollectionDescriptorByName(relationshipName);
> >>        }
> >>
> >>        if (rd == null) {
> >>            Log.error(this, "loadRelationship - couldn't find
relationship
> >>'" + relationshipName + "'");
> >>        } else {
> >>            PersistentField persistentField = rd.getPersistentField();
> >>            Object currentValue = persistentField.get(o);
> >>            if (currentValue == null) {
> >>                if (rd instanceof CollectionDescriptor) {
> >>                    persistentField.set(o,
> >
> > getCollectionRelationship(broker,
> >
> >>o, relationshipName));
> >>                } else {
> >>                    persistentField.set(o, getReferencedObject(broker,
o,
> >>relationshipName));
> >>                }
> >>            } else {
> >>                Log.debug(this, "loadRelationship - relationship '" +
> >>relationshipName + "' already loaded on " + o);
> >>            }
> >>        }
> >>        broker.close();
> >>    }
> >>
> >>
> >>   /**
> >>     * Get Foreign key query for 1:n collection, by name
> >>     *
> >>     * Largely copied from
> >>org.apache.ojb.broker.singlevm.PersitenceBrokerImpl
> >>     *
> >>     * @return org.apache.ojb.broker.query.Query
> >>     * @param obj
> >>     * @param collectionName name of the collection to build the query
for
> >>     */
> >>    private Query getForeignKeyQuery(PersistenceBroker broker, Object
obj,
> >>String collectionName) {
> >>        ClassDescriptor cld = broker.getClassDescriptor(obj.getClass());
> >>        CollectionDescriptor cod =
> >>cld.getCollectionDescriptorByName(collectionName);
> >>        ClassDescriptor refCld =
> >>broker.getClassDescriptor(cod.getItemClass());
> >>
> >>        Object[] values = cld.getKeyValues(obj);
> >>        FieldDescriptor[] fields =
> >>cod.getForeignKeyFieldDescriptors(refCld);
> >>
> >>        Criteria criteria = new Criteria();
> >>        for (int i = 0; i < fields.length; i++) {
> >>            FieldDescriptor fld = (FieldDescriptor) fields[i];
> >>            criteria.addEqualTo(fld.getAttributeName(), values[i]);
> >>        }
> >>
> >>        if (cod.getOrderBy() != null) {
> >>            criteria.addOrderBy(cod.getOrderBy(), cod.isAscending());
> >>        }
> >>
> >>        Query query = QueryFactory.newQuery(refCld.getClassOfObject(),
> >>criteria, true);
> >>
> >>        return query;
> >>    }
> >>
> >>    private Collection getCollectionRelationship(PersistenceBroker
broker,
> >>Object o, String collectionName) {
> >>        ClassDescriptor cld = broker.getClassDescriptor(o.getClass());
> >>        CollectionDescriptor cod =
> >>cld.getCollectionDescriptorByName(collectionName);
> >>        Class collectionClass = cod.getCollectionClass();
> >>        Collection results = null;
> >>
> >>        Query fkQuery =
> >>getForeignKeyQuery(PersistenceBrokerFactory.defaultPersistenceBroker(),
o,
> >>collectionName);
> >>        if (collectionClass == null) {
> >>            results = broker.getCollectionByQuery(fkQuery);
> >>        } else {
> >>            results = (Collection)
> >>broker.getCollectionByQuery(collectionClass, fkQuery);
> >>        }
> >>        return results;
> >>    }
> >>
> >>    /**
> >>     * Largely copied from
> >>org.apache.ojb.broker.singlevm.PersitenceBrokerImpl
> >>     * @param obj
> >>     * @param referenceName
> >>     * @return
> >>     */
> >>    private Object getReferencedObject(PersistenceBroker broker, Object
> >
> > obj,
> >
> >>String referenceName) {
> >>        ClassDescriptor cd = broker.getClassDescriptor(obj.getClass());
> >>        ObjectReferenceDescriptor rds =
> >>cd.getObjectReferenceDescriptorByName(referenceName);
> >>        Class referencedClass = rds.getItemClass();
> >>        // ensure that top-level extents are used for Identities
> >>
> >>        referencedClass =
> >>broker.getDescriptorRepository().getExtentClass(referencedClass);
> >>
> >>        Object[] pkVals = rds.getForeignKeyValues(obj, cd);
> >>
> >>        boolean allPkNull = true;
> >>
> >>        // BRJ : check if we have non null pk values
> >>        for (int i = 0; i < pkVals.length; i++) {
> >>            if (pkVals[i] != null) {
> >>                allPkNull = false;
> >>                break;
> >>            }
> >>        }
> >>
> >>        // BRJ : if all pk values are null there's no referenced object
> >>        if (allPkNull) {
> >>            return null;
> >>        } else {
> >>            Identity id = new Identity(referencedClass, pkVals);
> >>            return broker.getObjectByIdentity(id);
> >>        }
> >>    }
> >>
> >>
> >>
> >>
> >>
> >>
> >>>-----Original Message-----
> >>>From: Max Rydahl Andersen [mailto:[EMAIL PROTECTED]]
> >>>Sent: 09 September 2002 09:35
> >>>To: OJB Users List
> >>>Subject: Re: Anyone up for a "challange" ? :)
> >>>
> >>>
> >>>Willingly to share the code - just to see it ? :)
> >>>
> >>>/max
> >>>
> >>>----- Original Message -----
> >>>From: "Charles Anthony" <[EMAIL PROTECTED]>
> >>>To: "'OJB Users List'" <[EMAIL PROTECTED]>
> >>>Sent: Monday, September 09, 2002 9:31 AM
> >>>Subject: RE: Anyone up for a "challange" ? :)
> >>>
> >>>
> >>>
> >>>>[snippety-snip]
> >>>>
> >>>>>crystal clear! This relationship helper is something I have thought
> >>>>>about in the last week. As all needed functionality is already
> >>>>>implemented in the PersistenceBroker it will be pretty easy
> >>>>
> >>>to expose
> >>>
> >>>>>such a feature in the public API.
> >>>>>I will put this feature on my personal todo list.
> >>>>
> >>>>Cool; spookily, I have a RelationshipHelper class, that has a
> >>>>lazilyLoadRelationship(Object o, String relationshipName),
> >>>
> >>>which basically
> >>>
> >>>>delegates clones of the appropriate methods on the PB. It'd
> >>>
> >>>be good to
> >>>kick
> >>>
> >>>>out the cut-n-paste code re-use !
> >>>>
> >>>>Chers,
> >>>>
> >>>>Charles.
> >>>>
> >>>>
> >>>>This email and any attachments are strictly confidential and
> >>>
> >>>are intended
> >>>
> >>>>solely for the addressee. If you are not the intended
> >>>
> >>>recipient you must
> >>>
> >>>>not disclose, forward, copy or take any action in reliance
> >>>
> >>>on this message
> >>>
> >>>>or its attachments. If you have received this email in error
> >>>
> >>>please notify
> >>>
> >>>>the sender as soon as possible and delete it from your
> >>>
> >>>computer systems.
> >>>
> >>>>Any views or opinions presented are solely those of the
> >>>
> >>>author and do not
> >>>
> >>>>necessarily reflect those of HPD Software Limited or its affiliates.
> >>>>
> >>>> At present the integrity of email across the internet cannot be
> >>>
> >>>guaranteed
> >>>
> >>>>and messages sent via this medium are potentially at risk.
> >>>
> >>>All liability
> >>>
> >>>>is excluded to the extent permitted by law for any claims
> >>>
> >>>arising as a re-
> >>>
> >>>>sult of the use of this medium to transmit information by or to
> >>>>HPD Software Limited or its affiliates.
> >>>>
> >>>>
> >>>>
> >>>>--
> >>>>To unsubscribe, e-mail:
> >>>
> >>><mailto:[EMAIL PROTECTED]>
> >>>
> >>>>For
> >>>
> >>>additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> >>>
> >>>>
> >>>
> >>>--
> >>>To unsubscribe, e-mail:
> >>><mailto:[EMAIL PROTECTED]>
> >>>For
> >>>additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> >>>
> >>
> >>This email and any attachments are strictly confidential and are
intended
> >>solely for the addressee. If you are not the intended recipient you must
> >>not disclose, forward, copy or take any action in reliance on this
message
> >>or its attachments. If you have received this email in error please
notify
> >>the sender as soon as possible and delete it from your computer systems.
> >>Any views or opinions presented are solely those of the author and do
not
> >>necessarily reflect those of HPD Software Limited or its affiliates.
> >>
> >> At present the integrity of email across the internet cannot be
> >
> > guaranteed
> >
> >>and messages sent via this medium are potentially at risk.  All
liability
> >>is excluded to the extent permitted by law for any claims arising as a
re-
> >>sult of the use of this medium to transmit information by or to
> >>HPD Software Limited or its affiliates.
> >>
> >>
> >>
> >>--
> >>To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> >>For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
> >>
> >
> >
> > --
> > To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
> >
> >
> >
> >
>
>
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to