The data in table LabsUser (the Parent) contains:
Code:
LABSUSERID FIRSTNAME LASTNAME
---------- -------------------- --------------------
robi Robert HAUF
schur Frank SCHUR
razali Razali AHMAD
loh Vincent LOH
duncan Tim DUNCAN
garnett Kevin GARNETT
robinson David ROBINSON
allen Ray ALLEN
stockton Joh STOCKTON
The full mapping for LabsUser.hbm.xml is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hibtest.LabsUser" table="labsuser">
<id name="labsUserId" column="labsuserid">
<generator class="assigned"/>
</id>
<property name="firstName" type="string" />
<property name="lastName" type="string" />
<bag name="userRolesList" inverse="true" lazy="false" cascade="all" order-by="labsRoleId">
<key column="labsUserId" />
<one-to-many class="hibtest.UserRoleRelation" />
</bag>
</class>
</hibernate-mapping>
The data in table UserRoleRelation (the Child) contains:
Code:
LABSROLEID LABSUSERID
---------- ----------
CR loh
AD loh
AP loh
PC loh
AD robi
AP robinson
PC duncan
AP stockton
PC garnett
PC allen
The full mapping for UserRoleRelation.hbm.xml is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hibtest.UserRoleRelation" table="userrolerelation">
<id name="labsRoleId" column="labsRoleId" >
<generator class="assigned" />
</id>
<many-to-one name="LabsUser" column="labsUserId" not-null="true" />
</class>
</hibernate-mapping>
When I execute the following:
Code:
Session session = null;
try {
session = sf.openSession();
List lp = session.find("from UserRoleRelation where labsRoleId like 'PC'");
for (int i = 0; i < lp.size(); i++) {
System.out.println(((UserRoleRelation) lp.get(i)).getLabsUser().getLabsUserId());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null)
session.close();
}
It retrieves only the LabUserId = 'loh' even though there are 4 items in the list.