You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by frigo <pr...@gmail.com> on 2010/07/28 20:46:09 UTC

Sorting of to many relation Lists

Hi!

My problem/question is pretty simple, but somehow I didn't manage to find
the answer to it anywhere.
I have a to many relation in my data model, the result of which is stored in
a List on the java side.

I want to make sure that the objects in this List are ordered by a specific
attribute(column) of the other table? How can I do that without having to
sort the List every time I get them? I want to see an "ORDER BY <something>"
at the end of the automatic query!

I know how to use ordering in SelectQuery-s, but I want ordering when I call
one of the automatically generated List<Something> getSomethings(); methods.

Thanks for you hel! Sorry if the answer is just obvious and is in the second
line of the documentation :)!

Re: Sorting of to many relation Lists

Posted by Andrey Razumovsky <ra...@gmail.com>.
Hi,

It was already proposed in CAY-477 [1]. You can take a look at that patch
and apply it locally to have orderings for relationships.

[1] https://issues.apache.org/jira/browse/CAY-477

2010/7/28 frigo <pr...@gmail.com>

> Thank You for the answer, right know this is the way I do it, but is also
> exactly what I want to avoid. I want to let the database do all the
> ordering
> stuff.
>
> On Wed, Jul 28, 2010 at 9:03 PM, Michael Gentry <mgentry@masslight.net
> >wrote:
>
> > It is fairly easy to create a cover method to sort them.  For example,
> > if you have a Department ->> Person to-many relationship, you can do
> > something like this in your Department.java:
> >
> > public List<Person> getOrderedPersons()
> > {
> >    List<Ordering> orderings = new ArrayList<Ordering>();
> >
> >    orderings.add(new Ordering(Person.LAST_NAME_PROPERTY,
> > SortOrder.ASCENDING));
> >    orderings.add(new Ordering(Person.FIRST_NAME_PROPERTY,
> > SortOrder.ASCENDING));
> >
> >    List<Person> results = new ArrayList<Person>(getPersons());
> >
> >    Ordering.orderList(results, orderings);
> >
> >    return results;
> > }
> >
> > mrg
> >
> >
> > On Wed, Jul 28, 2010 at 2:46 PM, frigo <pr...@gmail.com>
> wrote:
> > > Hi!
> > >
> > > My problem/question is pretty simple, but somehow I didn't manage to
> find
> > > the answer to it anywhere.
> > > I have a to many relation in my data model, the result of which is
> stored
> > in
> > > a List on the java side.
> > >
> > > I want to make sure that the objects in this List are ordered by a
> > specific
> > > attribute(column) of the other table? How can I do that without having
> to
> > > sort the List every time I get them? I want to see an "ORDER BY
> > <something>"
> > > at the end of the automatic query!
> > >
> > > I know how to use ordering in SelectQuery-s, but I want ordering when I
> > call
> > > one of the automatically generated List<Something> getSomethings();
> > methods.
> > >
> > > Thanks for you hel! Sorry if the answer is just obvious and is in the
> > second
> > > line of the documentation :)!
> > >
> >
>



-- 
Andrey

Re: Sorting of to many relation Lists

Posted by frigo <pr...@gmail.com>.
Thank You for the answer, right know this is the way I do it, but is also
exactly what I want to avoid. I want to let the database do all the ordering
stuff.

On Wed, Jul 28, 2010 at 9:03 PM, Michael Gentry <mg...@masslight.net>wrote:

> It is fairly easy to create a cover method to sort them.  For example,
> if you have a Department ->> Person to-many relationship, you can do
> something like this in your Department.java:
>
> public List<Person> getOrderedPersons()
> {
>    List<Ordering> orderings = new ArrayList<Ordering>();
>
>    orderings.add(new Ordering(Person.LAST_NAME_PROPERTY,
> SortOrder.ASCENDING));
>    orderings.add(new Ordering(Person.FIRST_NAME_PROPERTY,
> SortOrder.ASCENDING));
>
>    List<Person> results = new ArrayList<Person>(getPersons());
>
>    Ordering.orderList(results, orderings);
>
>    return results;
> }
>
> mrg
>
>
> On Wed, Jul 28, 2010 at 2:46 PM, frigo <pr...@gmail.com> wrote:
> > Hi!
> >
> > My problem/question is pretty simple, but somehow I didn't manage to find
> > the answer to it anywhere.
> > I have a to many relation in my data model, the result of which is stored
> in
> > a List on the java side.
> >
> > I want to make sure that the objects in this List are ordered by a
> specific
> > attribute(column) of the other table? How can I do that without having to
> > sort the List every time I get them? I want to see an "ORDER BY
> <something>"
> > at the end of the automatic query!
> >
> > I know how to use ordering in SelectQuery-s, but I want ordering when I
> call
> > one of the automatically generated List<Something> getSomethings();
> methods.
> >
> > Thanks for you hel! Sorry if the answer is just obvious and is in the
> second
> > line of the documentation :)!
> >
>

Re: Sorting of to many relation Lists

Posted by Michael Gentry <mg...@masslight.net>.
It is fairly easy to create a cover method to sort them.  For example,
if you have a Department ->> Person to-many relationship, you can do
something like this in your Department.java:

public List<Person> getOrderedPersons()
{
    List<Ordering> orderings = new ArrayList<Ordering>();

    orderings.add(new Ordering(Person.LAST_NAME_PROPERTY, SortOrder.ASCENDING));
    orderings.add(new Ordering(Person.FIRST_NAME_PROPERTY,
SortOrder.ASCENDING));

    List<Person> results = new ArrayList<Person>(getPersons());

    Ordering.orderList(results, orderings);

    return results;
}

mrg


On Wed, Jul 28, 2010 at 2:46 PM, frigo <pr...@gmail.com> wrote:
> Hi!
>
> My problem/question is pretty simple, but somehow I didn't manage to find
> the answer to it anywhere.
> I have a to many relation in my data model, the result of which is stored in
> a List on the java side.
>
> I want to make sure that the objects in this List are ordered by a specific
> attribute(column) of the other table? How can I do that without having to
> sort the List every time I get them? I want to see an "ORDER BY <something>"
> at the end of the automatic query!
>
> I know how to use ordering in SelectQuery-s, but I want ordering when I call
> one of the automatically generated List<Something> getSomethings(); methods.
>
> Thanks for you hel! Sorry if the answer is just obvious and is in the second
> line of the documentation :)!
>