-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: many-to-many relation, count(*) query
PostPosted: Tue Feb 01, 2005 11:40 am 
Newbie

Joined: Mon Jan 17, 2005 9:23 am
Posts: 15
Hello!

I have a question. I have two entities, say, item and shop, in a many-to-many relation. Let's assume shop A sells items 1, 2, 3 but not 4. While shop B sells items 2, 3, 4 but not 1. Entity Shop has a set of Items, and Item has a set of Shops as property.

Now I want to query two things using HQL:

1. give me the number (count(*)) of items sold at shop A.
2. give me the first 20 items from a start index, say 147, so they can be displayed on the webpage, ordered by their name

Now, the same thing for ALL items is quite easy:

Code:
Query q1 = hibernateSes.createQuery("select count(i) from Item as i");
Query q2 = hibernateSes.createQuery("select i from Item as i order by i.name");
int num = q1.uniqueResult().intValue();
q2.setMaxResults(20);
q2.setFirstResult(147);


How can I achieve the same thing, only for Items sold at shop A ???

Thank you very much!
Bastian

Hibernate version: 2.1.7


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 2:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
please read the documentation about HQL syntax, especially what "join" does.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 4:52 am 
Newbie

Joined: Mon Jan 17, 2005 9:23 am
Posts: 15
Thanks for your answer, I have now done it like so:

Code:
Query q1 = hibernateSes.createQuery("select count(i) from Item as i join Shop as s where s.id=:shopid");
q1.setInteger("shopid", shop.getId().intValue() );
Query q2 = hibernateSes.createQuery("select i from Item as i join Shop as s where s.id=:shopid order by i.name");
q2.setInteger("shopid", shop.getId().intValue() );


Is this optimal, or can it be done better?

Greetings
Bastian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.