ordering and path expression

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ordering and path expression

Arnaud Garcia
Hi,

I would like to know how to use the ordering function with path
expression in my queries.
My objects are "Patient.class" with a list of "study.class" objects wich
has a studydate getter (a simple one to many relationship)

I want to retrieve my patients (by ID) with all the studies ordering by
date (the date is a filed of Study.class),

My code:
         Expression exp = ExpressionFactory.matchExp(patientid, "1234");
        query = new SelectQuery(Patient.class, exp);
        query.addPrefetch(studies);
        query.addOrdering(".....???...", true);   // maybe something
like:  toPatient.study.studydate
        List patients = context.performQuery(query);

many thanks for help

arnaud

Reply | Threaded
Open this post in threaded view
|

RE: ordering and path expression

Gentry, Michael (Contractor)
Ah, the orderings only apply to the returned object(s).  In your case,
you are asking for a Patient with an ID of 1234.  Only one object is
coming back in your List.  Your studies will be prefecthed, but will be
in "random" order (to the best of my knowledge).

The easiest way to handle this is to add a cover method in your subclass
(the non-underscore Patient.java) to do an in-memory sort of the studies
and use that method to get them in the order you want.

Look at
http://www.objectstyle.org/cayenne/userguide/fetch/orderings.html to see
how to do the in-memory sort.

/dev/mrg


-----Original Message-----
From: Arnaud Garcia [mailto:[hidden email]]
Sent: Wednesday, August 31, 2005 10:10 AM
To: [hidden email]
Subject: ordering and path expression


Hi,

I would like to know how to use the ordering function with path
expression in my queries.
My objects are "Patient.class" with a list of "study.class" objects wich

has a studydate getter (a simple one to many relationship)

I want to retrieve my patients (by ID) with all the studies ordering by
date (the date is a filed of Study.class),

My code:
         Expression exp = ExpressionFactory.matchExp(patientid, "1234");
        query = new SelectQuery(Patient.class, exp);
        query.addPrefetch(studies);
        query.addOrdering(".....???...", true);   // maybe something
like:  toPatient.study.studydate
        List patients = context.performQuery(query);

many thanks for help

arnaud

Reply | Threaded
Open this post in threaded view
|

Re: ordering and path expression

Arnaud Garcia
thanks for help Michael,

It still does not work, I try with a in Memory, but no difference ...
this is my latest version:

Expression exp = ExpressionFactory.matchExp(patientid, "1234");
query = new SelectQuery(Patient.class, exp);
query.addPrefetch(_Patient.STUDIES_PROPERTY);
// query.addOrdering("studies.studydt", true); // nice but not work,
however the sql request is ok, but certainely the fetch problem!!
 List patients = context.performQuery(query);

// So, now I try a in Memory sort: For each patient, I get all his
studies and try or order them by "studydate" (the getter or study.class),
//      
for (int i = 0; i < patients.size(); i++) {
            Patient p = (Patient) patients.get(i);
            Ordering ordering = new Ordering("studydate", true); //
Instead of the getter studydate I tried with studydt (obj-entity
attribute) but no diff
            ordering.orderList(p.getStudies());
        }


any idea ?
thanks
arnaud


Gentry, Michael (Contractor) a ?crit :

>Ah, the orderings only apply to the returned object(s).  In your case,
>you are asking for a Patient with an ID of 1234.  Only one object is
>coming back in your List.  Your studies will be prefecthed, but will be
>in "random" order (to the best of my knowledge).
>
>The easiest way to handle this is to add a cover method in your subclass
>(the non-underscore Patient.java) to do an in-memory sort of the studies
>and use that method to get them in the order you want.
>
>Look at
>http://www.objectstyle.org/cayenne/userguide/fetch/orderings.html to see
>how to do the in-memory sort.
>
>/dev/mrg
>
>
>-----Original Message-----
>From: Arnaud Garcia [mailto:[hidden email]]
>Sent: Wednesday, August 31, 2005 10:10 AM
>To: [hidden email]
>Subject: ordering and path expression
>
>
>Hi,
>
>I would like to know how to use the ordering function with path
>expression in my queries.
>My objects are "Patient.class" with a list of "study.class" objects wich
>
>has a studydate getter (a simple one to many relationship)
>
>I want to retrieve my patients (by ID) with all the studies ordering by
>date (the date is a filed of Study.class),
>
>My code:
>         Expression exp = ExpressionFactory.matchExp(patientid, "1234");
>        query = new SelectQuery(Patient.class, exp);
>        query.addPrefetch(studies);
>        query.addOrdering(".....???...", true);   // maybe something
>like:  toPatient.study.studydate
>        List patients = context.performQuery(query);
>
>many thanks for help
>
>arnaud
>
>
>  
>

Reply | Threaded
Open this post in threaded view
|

RE: ordering and path expression

Gentry, Michael (Contractor)
In reply to this post by Arnaud Garcia
I don't believe:

  ordering.orderList(p.getStudies());

Will actually update the DataObject's list.  So, the next time you do a p.getStudies(), you'll get the original list in it's original order.

That's why I was suggesting using a cover method in your Patient class.  Something like:

public List getStudies()
{
  List studies = super.getStudies();
  Ordering ordering = new Ordering("studydate", true);
  ordering.orderList(studies);
  return studies;
}

Of course, you could optimize that a bit, too (cache the sorted list).  You'd also want to cover the addTo/etc methods if you do cache things, so you could keep the cache in-sync.

/dev/mrg


-----Original Message-----
From: Arnaud Garcia [mailto:[hidden email]]
Sent: Wednesday, August 31, 2005 12:05 PM
To: [hidden email]
Subject: Re: ordering and path expression


thanks for help Michael,

It still does not work, I try with a in Memory, but no difference ...
this is my latest version:

Expression exp = ExpressionFactory.matchExp(patientid, "1234");
query = new SelectQuery(Patient.class, exp);
query.addPrefetch(_Patient.STUDIES_PROPERTY);
// query.addOrdering("studies.studydt", true); // nice but not work,
however the sql request is ok, but certainely the fetch problem!!
 List patients = context.performQuery(query);

// So, now I try a in Memory sort: For each patient, I get all his
studies and try or order them by "studydate" (the getter or study.class),
//      
for (int i = 0; i < patients.size(); i++) {
            Patient p = (Patient) patients.get(i);
            Ordering ordering = new Ordering("studydate", true); //
Instead of the getter studydate I tried with studydt (obj-entity
attribute) but no diff
            ordering.orderList(p.getStudies());
        }


any idea ?
thanks
arnaud


Gentry, Michael (Contractor) a écrit :

>Ah, the orderings only apply to the returned object(s).  In your case,
>you are asking for a Patient with an ID of 1234.  Only one object is
>coming back in your List.  Your studies will be prefecthed, but will be
>in "random" order (to the best of my knowledge).
>
>The easiest way to handle this is to add a cover method in your subclass
>(the non-underscore Patient.java) to do an in-memory sort of the studies
>and use that method to get them in the order you want.
>
>Look at
>http://www.objectstyle.org/cayenne/userguide/fetch/orderings.html to see
>how to do the in-memory sort.
>
>/dev/mrg
>
>
>-----Original Message-----
>From: Arnaud Garcia [mailto:[hidden email]]
>Sent: Wednesday, August 31, 2005 10:10 AM
>To: [hidden email]
>Subject: ordering and path expression
>
>
>Hi,
>
>I would like to know how to use the ordering function with path
>expression in my queries.
>My objects are "Patient.class" with a list of "study.class" objects wich
>
>has a studydate getter (a simple one to many relationship)
>
>I want to retrieve my patients (by ID) with all the studies ordering by
>date (the date is a filed of Study.class),
>
>My code:
>         Expression exp = ExpressionFactory.matchExp(patientid, "1234");
>        query = new SelectQuery(Patient.class, exp);
>        query.addPrefetch(studies);
>        query.addOrdering(".....???...", true);   // maybe something
>like:  toPatient.study.studydate
>        List patients = context.performQuery(query);
>
>many thanks for help
>
>arnaud
>
>
>  
>