-->
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: Did I miss something? newbie one-to-many question
PostPosted: Fri Jan 13, 2006 4:58 am 
Newbie

Joined: Fri Jan 13, 2006 4:19 am
Posts: 4
Hi!
Trying to implement the examples from the "Hibernate in Action", making a simple bidirectional one-to-many relation: the tipical ITEM-BID problem.

In the database I have one Item with two bids, one bid amount = 10, one with amount=20.

Wants to have the Item with bid.amount = 20. But as a result list I get is the Item with the two bids.

How can I get the Item with only one bid with the amount=20?

Hibernate version:
Hibernate-Version: 3.1.

Mapping documents:
ITEM:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="entities.Item" table="ITEM">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
       
        <property name="name"/>
       
       <property
           name="initialPrice"
           type="long"
           column="INITIAL_PRICE"
       />
      
       <set name="bids"
          lazy="true"
          cascade="save-update"
          inverse="true">
          <key column="ITEM_ID"/>
          <one-to-many class="entities.Bid"/>
       </set>
    </class>
</hibernate-mapping>


BID:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="entities.Bid" table="BID">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
       <property
           name="amount"
           type="long"
           column="AMOUNT"
       />
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
List items = HibernateUtil.getSession().createCriteria(Item.class)
         .createCriteria("bids")
          .add(Expression.eq("amount", new Long("10")))
          .list();

Iterator itItems = items.iterator();
        while (itItems.hasNext()) {
            Item i = (Item)itItems.next();
            Set bids = i.getBids();
            System.out.println("bids size: " + bids.size());
        }


Name and version of the database you are using:
Oracle 8

The generated SQL (show_sql=true):
Code:
09:39:20,989 DEBUG SQL:324 - select this_.ID as ID1_, this_.name as name4_1_, this_.INITIAL_PRICE as INITIAL3_4_1_, bid1_.ID as ID0_, bid1_.AMOUNT as AMOUNT5_0_ from ITEM this_, BID bid1_ where this_.ID=bid1_.ITEM_ID and bid1_.AMOUNT=?
Hibernate: select this_.ID as ID1_, this_.name as name4_1_, this_.INITIAL_PRICE as INITIAL3_4_1_, bid1_.ID as ID0_, bid1_.AMOUNT as AMOUNT5_0_ from ITEM this_, BID bid1_ where this_.ID=bid1_.ITEM_ID and bid1_.AMOUNT=?
09:39:21,067 DEBUG SQL:324 - select bids0_.ITEM_ID as ITEM3_1_, bids0_.ID as ID1_, bids0_.ID as ID0_, bids0_.AMOUNT as AMOUNT5_0_ from BID bids0_ where bids0_.ITEM_ID=?
Hibernate: select bids0_.ITEM_ID as ITEM3_1_, bids0_.ID as ID1_, bids0_.ID as ID0_, bids0_.AMOUNT as AMOUNT5_0_ from BID bids0_ where bids0_.ITEM_ID=?
bids size: 2


Thanx for helping!

Trans


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 8:10 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
First glance looks ok. What happens when you run the SQL in the DB

select this_.ID as ID1_, this_.name as name4_1_, this_.INITIAL_PRICE as INITIAL3_4_1_, bid1_.ID as ID0_, bid1_.AMOUNT as AMOUNT5_0_ from ITEM this_, BID bid1_ where this_.ID=bid1_.ITEM_ID and bid1_.AMOUNT=10


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 8:16 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Sorry - My bad.

Look closely at the code.

Code:
List items = HibernateUtil.getSession().createCriteria(Item.class)
         .createCriteria("bids")
          .add(Expression.eq("amount", new Long("10")))
          .list();

This gets all the Items which have a bid on them with amount of 10

Code:
Iterator itItems = items.iterator();
        while (itItems.hasNext()) {
            Item i = (Item)itItems.next();
            Set bids = i.getBids();
            System.out.println("bids size: " + bids.size());
        }

This loops over those items. When you execute "i.getBids();" you can see that it is looking at all the bids for that item (see the last SQL statement). This is correct behaviour - you are asking the item to show you it's bids and it has indeed got 2 bids - it's just that when you do i.getBids(), it then goes and loads the other bids for the item.

Your logic here is actually "Give me all items that have a bid of 10" and then "Show me all bids for that item"

Does that make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 8:35 am 
Newbie

Joined: Fri Jan 13, 2006 4:19 am
Posts: 4
Ok! I got the point...
But than how can I get the Item, only with the bid amount = 10. Any time I call item.getBids() he will get me all the bids for the item.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 8:41 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
That's correct though.

You have one item with 2 bids. You search for your item and receive one result. You then ask it for it's bids - there are 2.

If you want to get the bid that is of value 10, then you search for the bid that has a value of 10, not search for the item that has a bid of value 10.

So I guess what you want to do is search for Bid that has a value of 10 and an item id of XYZ... although why you would ever do that I don't know

P.s. - don't forget the ratings ;o)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:08 am 
Newbie

Joined: Fri Jan 13, 2006 4:19 am
Posts: 4
That means it is not possible to get the Item with the Bit amount=10? I dont need the Bit with amount=10. I need the Item with bid = 10.(Ok, I know you can have it from Bid, but what if my application works with Item entities, and my method returns a list of Item entities. I dont want to go trough all the Bid entities and get the Items.).

In SQL would be this:
Quote:
SELECT i.* FROM Item i, Bid b where i.id = b.item_id and b.amount = 10


In Java i would expect to get one Item entity with one Bid entity in the bids set. (Even if I call the item.getBids() method)

Is that not possible?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:19 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Quote:
That means it is not possible to get the Item with the Bit amount=10?
You were already doing it!!!!


Code:
SELECT i.* FROM Item i, Bid b where i.id = b.item_id and b.amount = 10

This returns the item which has a bid of 10.

Code:
List items = HibernateUtil.getSession().createCriteria(Item.class)
         .createCriteria("bids")
          .add(Expression.eq("amount", new Long("10")))
          .list();


Also returns the item which has a bid of 10.

In your code though, you are then asking each item for all it's bids and thus issuing another sql which is effectively:
Code:
select * from bid where item_id="VALUE YOU GOT FROM THE ABOVE"


If you want to view the items with a bid value of 10 just do

Code:
List items = HibernateUtil.getSession().createCriteria(Item.class)
         .createCriteria("bids")
          .add(Expression.eq("amount", new Long("10")))
          .list();

// you have the items!!! - don't go asking for any bid info - you don't care
Iterator iter = items.iterator();
while (iter.hasNext) {
  Item item = (Item) iter.next();
  System.out.println(item.getXYZ()); // etc
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:36 am 
Newbie

Joined: Fri Jan 13, 2006 4:19 am
Posts: 4
I got it (finally) :-)
Thank you


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.