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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Criteria in One-to-Many Association
PostPosted: Thu Jul 27, 2006 10:12 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
I have one-to-many association between ORDER and ITEM.

ORDER.ID
ORDER.ITEMS

ITEM.ID
ITEM.PRICE

I want to find all the orders when ITEM.PRICE=100

This is my code

List tempSets = session.createCriteria(Order.class)
.createCriteria("items")
.add(Restrictions.eq("price",new Long(100)))
.list();

It finds correct number of Orders but when I ask for order.getItems() it gives me all the items for that order and what I need is the order only with items that match this restriction (price==100)

Hibernate generates this query:
select o.*, i.* from order o inner join item i on o.id=i.order_id where i.price=100

which is correct, but somehow later on it does select all items for this orders. Is there a way to restrict also association query?

How can I achieve it?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 10:47 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
List itemsList = session.createFilter( orderObject.getItems(), " where this.price=? " ).setParameter( 0, new Long( 100 ) ).list();

Would this help to get only those items with price=100?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 11:11 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
Yes sort of.. I mean I did it.. and it works, but I would expect hibernate to have a bit better way of doing that.. is there? :)

for (Iterator i=orders.iterator();i.hasNext();) {
Order tempOrder = (Order)i.next();
List items = (List)session.createFilter(tempOrder.getItems(), " where this.price=100" )
.setParameter( 0, new Long(100) )
.list();
tempOrder.setItems(items);
}

Thanks anyway!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 4:21 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Instead of iterating through Order you could use HQL join condition and specify parmeters for Order and Item in the same WHERE clause.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 4:22 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Instead of iterating through Order you could use HQL join condition and specify parmeters for Order and Item in the same WHERE clause. That would result in 1 SQL v/s 1 to n SQLs in the code snippet you pasted.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 4:23 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
anandbn wrote:
Instead of iterating through Order you could use HQL join condition and specify parmeters for Order and Item in the same WHERE clause. That would result in 1 SQL v/s 1 to n SQLs in the code snippet you pasted.


Can you please post sample of that..

And of course code I posted is very inneficient and I'm quite surprised there is not better way of doing it with Criteria object.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 4:59 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
You could so something like this:

Code:
from Order as ord inner join ord.items as item where
ord.date=:orderDate and item.price<1000
.

I beleive you can do a similar thing using Criteria queries as well. It might go something like this:

Code:
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "F%") )
.createCriteria("kittens")
.add( Restrictions.like("name", "F%") )
.list();


Replace cat and kitten with Order and Items appropriately. Refer to Section 15.4 of Reference guide.


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