-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL related to collections in many-to-many relationship
PostPosted: Mon Dec 06, 2004 8:24 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
Hibernate version: 2.1.7c

I'm having a problem in getting the proper HQL for the follwong senario:

Tables : ITEM (ItemID, name)
ORDER (OrderID, Description)
ITEM_ORDER (OrderID,ItemID)

I have done the many-to-many relationship mapping. (In this, the ITEM has a set of Orders and The Orders has a set of Items)
ITEM is defined as the inverse end. The records for ITEM_ORDER table gets inserted when calling the session.save(order) and works fine.


Now, how to write the HQL to find out all the Orders objects that contains Item "ITM_01" ? In other words, i need to get all the Orders objects, that has Item "ITM_01" in their Item set.

please help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 07, 2004 1:40 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
oh, i found it and so impressed to see how simple it is :

Code:
select order from ORDER as order,ITEM as item where item.itemID like 'ITM_01' and item in elements(order.items)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 07, 2004 2:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No. Use:

Code:
select order
from Order as order
  join order.items as item
where item.itemID like 'ITM_01'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 12:26 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
thanks Gavin.

I had a good look at the SQL query that was generated behind the query that i had used and the query that you have given.

The difference i saw was that mine was using a subquery whereas the use of Join did it without using a subquery. According to my knowledge use of sub queries are meant to be much efficient in terms of performance.

So, was my query not the best way, since it violates the hibernate concept in terms of database independance? (Not all databases support subqueries). Keen to know why it was incorrect.

thanks again
Shanika


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 3:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
The difference i saw was that mine was using a subquery whereas the use of Join did it without using a subquery. According to my knowledge use of sub queries are meant to be much efficient in terms of performance.

false, run the two queries with a database with a lot of datas, Gavin's query will run much faster.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 3:27 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
thanks. so it's the performance difference.

thanks for the prompt reply.


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