Ok, normally the hbm.xml of Department and Staff in this case would be
Code:
<class name="Department" table="Department">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="staffs">
<key column="department_id"/>
<one-to-many class="Staff"/>
</set>
</class>
<class name="Staff" table="Staff">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="department" column="department_id" not-null="true"/>
</class>
You would be able to query by doing the following:
Code:
Query query = session.createQuery( "FROM Department" );
Iterator it = query.iterate();
while( it.hasNext() )
{
Department dep = (Department) it.next();
Set staffs = dep.getStaffs();
Iterator itStaffs = staffs.iterator();
while( itStaffs.hasNext() )
{
.....
}
}
You should get the point :)
Otherwise, you can do scalar queries.
Code:
Query query = session.createQuery( "SELECT d.name, s.name FROM Department d INNER JOIN d.staffs AS s ORDER BY d.name, s.name" );
Iterator it = query.iterate();
while( it.hasNext() )
{
Object[] results = (Object[]) it.next();
String depName = (String) results[0];
String staffName = (String) results[1];
....
}