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