You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Nishant Neeraj <nn...@yahoo.com> on 2009/09/13 10:55:26 UTC

Sorting and Pagination in Many-to-Many relationship

I am having problem in sorting and paginating  many to many relationship objects.
Here is the DB relationship model.  http://i30.tinypic.com/jaz4wm.jpgI want to do Readers.getMagzines() that returns paginated resultset of page size, N.

I write a Readers.getMagzine() Method that pulls out List of subscriptions. In the method, I iterate through the list and inflating Magzines (subscriptions.getMagzines()) and injecting SubscriptionType to magzine object. And then I return the list. This works fine.
Now, I do not want to pull out the full list. Rather, I want do it page by page sort by say, MagzineName with page Size N. Not sure how to do it. Can some one give a hint?
ThanksNishant


      Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com

Re: Sorting and Pagination in Many-to-Many relationship

Posted by Nishant Neeraj <nn...@yahoo.com>.
Thanks for imformative link.


I have just started Cayenne and trying to get used to it. In the same set of exercise, I was trying to pull a list of Subscriptions (paginated) sorted by magzine name.


I did this, wondering if this is the most correct way to do it. (refer data model here  http://i30.tinypic.com/jaz4wm.jpg )


DataContext ctx = DataContext.createDataContext();
Readers r = (Readers)DataObjectUtils.objectForPK(ctx, Readers.class, 200);
Expression e = Expression.fromString("reader=$reader");
Map param = new HashMap(1);
param.put("reader",r);
SelectQuery q = new SelectQuery(Subscriptions.class,e.expWithParameters(param));		
Ordering o = new Ordering(Subscriptions.MAGZINE_PROPERTY+"."+Magzines.M_NAME_PROPERTY, true);

q.addOrdering(o);
List<Subscriptions> subs = ctx.performQuery(q);

- Nishant
--- On Wed, 16/9/09, Aristedes Maniatis <ar...@maniatis.org> wrote:

From: Aristedes Maniatis <ar...@maniatis.org>
Subject: Re: Sorting and Pagination in Many-to-Many relationship
To: user@cayenne.apache.org
Date: Wednesday, 16 September, 2009, 7:22 AM

On 16/09/09 6:22 AM, Nishant Neeraj wrote:
> A question regarding caching. When we use SQLTemplate or Expression, does the objects gets cached? I mean, these are very frequent used piece of code.... as soon as user logs in we will show his list of Magazines... if the object is not cached, we may have sluggish performance under load.

For improved caching you definitely want to be using Cayenne 3. And the answer is that Cayenne does a very good job of caching, but you'll want to read the documentation carefully to understand what is going on.

http://cayenne.apache.org/doc/caching-and-fresh-data.html


Ari Maniatis

-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A



      Now, send attachments up to 25MB with Yahoo! India Mail. Learn how. http://in.overview.mail.yahoo.com/photos

Re: Sorting and Pagination in Many-to-Many relationship

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 16/09/09 6:22 AM, Nishant Neeraj wrote:
> A question regarding caching. When we use SQLTemplate or Expression, does the objects gets cached? I mean, these are very frequent used piece of code.... as soon as user logs in we will show his list of Magazines... if the object is not cached, we may have sluggish performance under load.

For improved caching you definitely want to be using Cayenne 3. And the answer is that Cayenne does a very good job of caching, but you'll want to read the documentation carefully to understand what is going on.

http://cayenne.apache.org/doc/caching-and-fresh-data.html


Ari Maniatis

-- 

-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Sorting and Pagination in Many-to-Many relationship

Posted by Nishant Neeraj <nn...@yahoo.com>.
Thanks mrg. This helps a bit my understanding. There are two things. 


1. Subscription type is dynamic. Actually, we should be able to add subscription type without altering the code. 


2. The requirement is like this, I need to display paginated tabular list of magazines subscribed by a user. Each row has indication of type of subscription that reader had chosen. 


I guess, I need (a) paginated list of subscriptions (b) sorted by any fields available in Magazines or Subscription_Types table. I will inflate Magazines and SubscriptionTypes at display time.


A question regarding caching. When we use SQLTemplate or Expression, does the objects gets cached? I mean, these are very frequent used piece of code.... as soon as user logs in we will show his list of Magazines... if the object is not cached, we may have sluggish performance under load.


Help appreciated.



- Nishant
--- On Mon, 14/9/09, Michael Gentry <mg...@masslight.net> wrote:

From: Michael Gentry <mg...@masslight.net>
Subject: Re: Sorting and Pagination in Many-to-Many relationship
To: user@cayenne.apache.org
Date: Monday, 14 September, 2009, 8:23 PM

Hi Nishant,

First off, unless your Subscription Type is rather dynamic, you might
want to consider using an enum for it (especially if you are using
3.0):

http://cayenne.apache.org/doc/modeling-enumerations.html

Next, it sounds like what you are wanting is a list of magazines for a
given user.  Instead of starting at the user, you could start at the
magazines and put the sort order on it.  You essentially want to:
fetch magazines where subscriptions.readers.rname = your_reader order
by mname.

In Cayenne, it would be more like (and I'm guessing at your
relationship/attribute names):

Expression exp = Expression.fromString("subscriptions.readers.rname = $reader");
Map param = new HashMap(1);
param.put("reader", "reader's name");
SelectQuery query = new SelectQuery(Magazine.class,
exp.expWithParameters(param));
Ordering order = new Ordering(Magazine.MNAME_PROPERTY, true);
query.addOrdering(order);
List<Magazine> magazines = dataContext.performQuery(query);

mrg


On Sun, Sep 13, 2009 at 4:55 AM, Nishant Neeraj <nn...@yahoo.com> wrote:
> I am having problem in sorting and paginating  many to many relationship objects.
> Here is the DB relationship model.  http://i30.tinypic.com/jaz4wm.jpgI want to do Readers.getMagzines() that returns paginated resultset of page size, N.
>
> I write a Readers.getMagzine() Method that pulls out List of subscriptions. In the method, I iterate through the list and inflating Magzines (subscriptions.getMagzines()) and injecting SubscriptionType to magzine object. And then I return the list. This works fine.
> Now, I do not want to pull out the full list. Rather, I want do it page by page sort by say, MagzineName with page Size N. Not sure how to do it. Can some one give a hint?
> ThanksNishant
>
>
>      Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com



      Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com

Re: Sorting and Pagination in Many-to-Many relationship

Posted by Michael Gentry <mg...@masslight.net>.
Hi Nishant,

First off, unless your Subscription Type is rather dynamic, you might
want to consider using an enum for it (especially if you are using
3.0):

http://cayenne.apache.org/doc/modeling-enumerations.html

Next, it sounds like what you are wanting is a list of magazines for a
given user.  Instead of starting at the user, you could start at the
magazines and put the sort order on it.  You essentially want to:
fetch magazines where subscriptions.readers.rname = your_reader order
by mname.

In Cayenne, it would be more like (and I'm guessing at your
relationship/attribute names):

Expression exp = Expression.fromString("subscriptions.readers.rname = $reader");
Map param = new HashMap(1);
param.put("reader", "reader's name");
SelectQuery query = new SelectQuery(Magazine.class,
exp.expWithParameters(param));
Ordering order = new Ordering(Magazine.MNAME_PROPERTY, true);
query.addOrdering(order);
List<Magazine> magazines = dataContext.performQuery(query);

mrg


On Sun, Sep 13, 2009 at 4:55 AM, Nishant Neeraj <nn...@yahoo.com> wrote:
> I am having problem in sorting and paginating  many to many relationship objects.
> Here is the DB relationship model.  http://i30.tinypic.com/jaz4wm.jpgI want to do Readers.getMagzines() that returns paginated resultset of page size, N.
>
> I write a Readers.getMagzine() Method that pulls out List of subscriptions. In the method, I iterate through the list and inflating Magzines (subscriptions.getMagzines()) and injecting SubscriptionType to magzine object. And then I return the list. This works fine.
> Now, I do not want to pull out the full list. Rather, I want do it page by page sort by say, MagzineName with page Size N. Not sure how to do it. Can some one give a hint?
> ThanksNishant
>
>
>      Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com

Re: Sorting and Pagination in Many-to-Many relationship

Posted by Nishant Neeraj <nn...@yahoo.com>.
the DB relationship model is     


http://i30.tinypic.com/jaz4wm.jpg
- Nishant

--- On Sun, 13/9/09, Nishant Neeraj <nn...@yahoo.com> wrote:

From: Nishant Neeraj <nn...@yahoo.com>
Subject: Sorting and Pagination in Many-to-Many relationship
To: "Cayenne Mailing List" <us...@cayenne.apache.org>
Date: Sunday, 13 September, 2009, 2:25 PM

I am having problem in sorting and paginating  many to many relationship objects.
Here is the DB relationship model.  http://i30.tinypic.com/jaz4wm.jpgI want to do Readers.getMagzines() that returns paginated resultset of page size, N.

I write a Readers.getMagzine() Method that pulls out List of subscriptions. In the method, I iterate through the list and inflating Magzines (subscriptions.getMagzines()) and injecting SubscriptionType to magzine object. And then I return the list. This works fine.
Now, I do not want to pull out the full list. Rather, I want do it page by page sort by say, MagzineName with page Size N. Not sure how to do it. Can some one give a hint?
ThanksNishant


      Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com


      See the Web&#39;s breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/