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.  [ 5 posts ] 
Author Message
 Post subject: Need Help in query a bi-directional many-to-one relationship
PostPosted: Thu Nov 02, 2006 12:43 pm 
Newbie

Joined: Sun Oct 15, 2006 11:20 pm
Posts: 13
I am reading the Hibernate-In-Action book. In chapter 3, it has a bi-directional many-to-one relationship. Bid and Item.

Code:
public class Bid {
   private Item item;
   private float amount;

}

public class Item {
   private Set bids = new HashSet();
}



My question is if I want query an Item by ID and Bid.amount = 10.0, what is the most efficient to do it?

One thing I think of is this:
Code:
Item item = itemDAO.getItemByID(itemId);
// loop thru in java code to find the amout = 10.0
Set itemBids = item.getBids();
for (Iterator iter = itemBids.iterator(); iter.hasNext();) {
   (Bid) element = (Bid) iter.next();
   if (element.amount == 10.0) {
     // found ....
   ....
}
            
}



Is there a better way to do it? I think doing this will require loading all bids of that item to the memory from database. I am trying to come up a better way.

Thank you.


Top
 Profile  
 
 Post subject: Maybe you can use a Query for this...
PostPosted: Thu Nov 02, 2006 2:18 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
You have:
Item item = itemDAO.getItemByID(itemId);

then, one query could be:

Query q = sess.createQuery("from Bid b where b.amount =10 and b.oneItem = :myitem");
q.setEntity("myitem",item);
List ListBids = q.list();

then you can iterate the list.

For example, if in your Bid.hbm.xml mapping file, you have
<many-to-one
name="oneItem"
class="encuestas.Item"
> .....
you must put the same name of the class in the name of propiety of the query for quering a entity (b.oneItem)

This help you???


Top
 Profile  
 
 Post subject: little correction
PostPosted: Thu Nov 02, 2006 2:24 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Sorry, in
<many-to-one
name="oneItem"
class="your_package.Item"
> .....

your_package is just that, el name of your package, where Item is.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 3:20 pm 
Newbie

Joined: Sun Oct 15, 2006 11:20 pm
Posts: 13
Thanks. But which query is faster:
Is this faster than your suggestion:
Code:
Query q = sess.createQuery("from Bid b where b.amount =10 and b.oneItem.id = :myitemid");
q.setLong("myitemid",item.getId().longValue());
List ListBids = q.list();



your original:
Code:
Query q = sess.createQuery("from Bid b where b.amount =10 and b.oneItem = :myitem");
q.setEntity("myitem",item);
List ListBids = q.list();



or they don't make any difference?

The when I see the native mysql that query you generated. It has more conditions in the query (every property of item has a condition). I wonder if that results in a slower query.

Thank you.


Top
 Profile  
 
 Post subject: faster???
PostPosted: Thu Nov 02, 2006 5:20 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
If my solution generate more conditions, maybe it's more direct to acces database, Your

Query q = sess.createQuery("from Bid b where b.amount =10 and b.oneItem.id = :myitemid");
q.setLong("myitemid",item.getId().longValue());
List ListBids = q.list();

retrieve id again, in item.getId.longValue(), I think, is redundant, but I just think its better of your

Item item = itemDAO.getItemByID(itemId);
// loop thru in java code to find the amout = 10.0
Set itemBids = item.getBids();
for (Iterator iter = itemBids.iterator(); iter.hasNext();) {
(Bid) element = (Bid) iter.next();
if (element.amount == 10.0) {
// found ....
....
}

}

Maybe we need calculate what order have all


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