Hi,
I have a simplified version of te auction database from the "Hibernate In Action" book, with a 1-many relationship between item and bid. I have 6 items in my item table, 5 with the description of "old guitar" and one with the description of "Strat". When I do a query by constraint on the description "old guitar" i get a list of 15 items! Each item is repeated 3 times. When I do a query by example on the same criteria, I get no items returned. However, a QBE on the price of the item (12000.0F) returns 2 items, even though there is only one in the table.
What's going on?
Thanks!
Code:
[b]Hibernate version: 2.1.8[/b]
[b]Mapping documents: [u]Auction.hbm.xml[/u]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="domain.Item" table="item">
<id name="id" unsaved-value="0" column="ITEM_ID" type="long">
<generator class="identity" />
</id>
<set name="bids" cascade="all-delete-orphan" outer-join="true" inverse="true">
<key column="ITEM_ID" />
<one-to-many class="domain.Bid" />
</set>
<property name="description" type="java.lang.String" column="description" />
<property name="price" type="float" column="price" />
</class>
<class name="domain.Bid" table="bid">
<id name="id" unsaved-value="0" column="BID_ID" type="long">
<generator class="identity" />
</id>
<many-to-one name="item" column="ITEM_ID" class="domain.Item" />
<property name="amount" column="amount" type="float" />
</class>
</hibernate-mapping>
[u]hibernate.cfg.xml[/u]
<?xml version='1.0' encoding='UTF-8'?>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="domain/Auction.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[b]Code between sessionFactory.openSession() and session.close():
public void testRetrieveByQBC() throws HibernateException {
Session session = getSession();
Criteria criteria = session.createCriteria(Item.class);
criteria.add(Expression.eq("description", "old guitar"));
List guitars = criteria.list();
assertNotNull(guitars);
assertTrue(guitars.size() > 0);
System.out.println("Size of QBC list: " + guitars.size());
closeSession();
}
public void testRetrieveByQBEDesc() throws HibernateException
{
Session session = getSession();
Item item = new Item();
item.setDescription("old guitar");
Criteria criteria = session.createCriteria(Item.class);
criteria.add(Example.create(item));
List result = criteria.list();
assertNotNull(result);
assertTrue(result.size() > 0);
System.out.println("Size of QBE list: " + result.size());
closeSession();
}
[/b]
[b]Full stack trace of any exception that occurs:[/b]
[b]Name and version of the database you are using: MySQL 4.0.20a[/b]
[b]The generated SQL (show_sql=true):
[u]QBC[/u]
Hibernate: select this.ITEM_ID as ITEM_ID1_, this.description as descript2_1_, this.price as price1_, bids1_.ITEM_ID as ITEM_ID__, bids1_.BID_ID as BID_ID__, bids1_.BID_ID as BID_ID0_, bids1_.ITEM_ID as ITEM_ID0_, bids1_.amount as amount0_ from item this left outer join bid bids1_ on this.ITEM_ID=bids1_.ITEM_ID where this.description=?
[u]QBE[/u]
Hibernate: select this.ITEM_ID as ITEM_ID1_, this.description as descript2_1_, this.price as price1_, bids1_.ITEM_ID as ITEM_ID__, bids1_.BID_ID as BID_ID__, bids1_.BID_ID as BID_ID0_, bids1_.ITEM_ID as ITEM_ID0_, bids1_.amount as amount0_ from item this left outer join bid bids1_ on this.ITEM_ID=bids1_.ITEM_ID where (this.description=? and this.price=?)
[/b]
[b]Debug level Hibernate log excerpt:[/b]
[/code][/u]