-->
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.  [ 8 posts ] 
Author Message
 Post subject: HQL Help
PostPosted: Tue May 24, 2005 5:00 pm 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
Ok I have no idea how to do this in HQL.

I have a product. The product belongs to a company. The company belongs to Sectors. How do I get all of the products in a given MarketSector?

This is my thought but I don't know how to write it

Code:
FROM Model.Product p
WHERE p.Company.MarketSectors[???].Name = "Plastics"


MarketSectors is a bag, it could be a map but either way it is a collection of MarketSectors.

In SQL I would have used JOINS but I tried using them and I got this error about needing a path expression.

Any thoughts?
Thanks.
dru


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 2:23 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I believe you just use MarketSectors even when referring to a single element of the collection, so remove those brackets with question marks and try it :) I know it feels a bit unnatural, but if you imagine the SQL behind it, it makes more sense.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 10:44 am 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
MarketSectors is an IList by the way.

I tried what you siad and got this error

NHibernate.QueryException: expecting 'elements' or 'indicies'

Any thoughts, I am having trouble googling it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 10:53 am 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
I have a path to the solution but I am still confused
Here is the Hibernate documentation on it

http://www.hibernate.org/hib_docs/refer ... ryhql.html

I am now just confused about elements and indicies which to use and how.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:49 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Umm, try this:
Code:
from Model.Product p join p.Company.MarketSectors sector
where sector.Name = 'Plastics'


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:50 am 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
Another wrinkle is that I forgot that MarketSectors is a Many to Many type relationship so the actual HQL is more like this

Code:
FROM Model.Product p
WHERE p.Company.MarketSectors.Sector.Name = "Plastics"


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:55 am 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
sergey:
Right on!

That has me almost there. Now my problem is that I am returning an ArrayList where each entry has two entries one with the Product theother with the market sector.

Anyway I can remove the market sector?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 12:03 pm 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
Here is what I did. The perf on this has to horrible but I will see.

Code:
select p
from Model.Product p join p.Company.MarketSectors sector
where sector.Name = 'Plastics'


Thanks for the help. This at least has me started.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.