Hibernate version:Hibernate-3.1.3
Name and version of the database used: Mysql 4.1.15-nt
Database state: UserData table has more number of entries than Users table.
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.test.user.User" table="users" proxy="com.test.user.User">
<id name="userId" type="java.lang.Integer" column="UserId">
<generator class="native" />
</id>
<set name="userdata" lazy="true" cascade="all" inverse="true" [b]fetch="join"[/b]>
<key><column name="UserId" /></key>
<one-to-many class="com.test.user.Userdata"/>
</set>
</class>
<class name="com.test.user.Userdata" table="userdata" proxy="com.test.user.Userdata">
<id name="userDataId" type="java.lang.Integer" column="UserDataId">
<generator class="native" />
</id>
<many-to-one name="user" class="com.test.user.User" not-null="true">
<column name="UserId" />
</many-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): Case1:
Code:
Criteria criteria = session.createCriteria(User.class);
System.out.println("Total Users:="+criteria.list().size());
Case2:
Code:
Query queryObject = session.createQuery("from User");
System.out.println("Total Users:="+queryObject.list().size());
Description of an error: Ideally, both cases should give same result, but results are not as expected.
Case 1 Gives total rows in UserData table.
Case 2 Gives total rows in Users table as expected.
If we remove 'fetch="join"' from mapping, both cases work fine and will give total rows in Users table.
The generated SQL (show_sql=true): Case 1:
Code:
Hibernate: select this_.UserId as UserId0_1_, userdata2_.UserId as UserId3_, userdata2_.UserDataId as UserDataId3_, userdata2_.UserDataId as UserDataId1_0_, userdata2_.UserId as UserId1_0_ from users this_ left outer join userdata userdata2_ on this_.UserId=userdata2_.UserId
Case 2:
Code:
Hibernate: select user0_.UserId as UserId0_ from users user0_
Can u tell me, why criteria.list() gives wrong result, if fetch="join" is set. Expected result for criteria.list() is size of Users table, and not the size of UserData table.