If the data is ont there in the database i.e. there is no Employee with an organization of 10, you won't enter the last while loop.
I have the following data in the database.
Code:
<dataset>
<ORGANIZATION orgid="10" orgName="TD Waterhouse" orgAddress="14 Kings Way"/>
<ORGANIZATION orgid="2" orgName="Kings Court" orgAddress="22 Mike Way"/>
<ORGANIZATION orgid="3" orgName="CIBC" orgAddress="20 Karls Road"/>
<EMPLOYEE empId="1" firstName="Mark" lastName="Brown" fk_orgid="2"/>
<EMPLOYEE empId="2" firstName="Karen" lastName="James" fk_orgid="2"/>
<EMPLOYEE empId="3" firstName="Phillip" lastName="Gayle" fk_orgid="2"/>
<EMPLOYEE empId="4" firstName="Guyan" lastName="Brissett" fk_orgid="10"/>
<EMPLOYEE empId="10" firstName="Rick" lastName="Dooley" fk_orgid="10"/>
</dataset>
The persistent classes I haven't changed, but here goes anyway
Code:
package hibernate;
import dg.base.BasePersistentObject;
public class Employee extends BasePersistentObject {
private Integer empId;
private String firstName;
private String lastName;
private Organization org;
public Employee(){ }
public Employee(Integer id, String fn, String ln, Organization fk){
this.empId = id;
this.firstName = fn;
this.lastName = ln;
this.org = fk;
}
/**
*
* @hibernate.id
* generator-class="native"
* column="empId"
*
* @return the id
*
*/
public Integer getEmpId() {
return empId;
}
/**
*
* @hibernate.property
* column="firstName"
*
* @return
*
*/
public String getFirstName() {
return firstName;
}
/**
*
* @hibernate.many-to-one
* column="fk_orgId"
*
* @return the organization of the employee
*
*/
public Organization getOrg() {
return org;
}
/**
*
* @hibernate.property
* column="lastName"
*
* @return
*
*/
public String getLastName() {
return lastName;
}
/**
* @param integer
*/
public void setEmpId(Integer integer) {
empId = integer;
}
/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}
/**
* @param organization
*/
public void setOrg(Organization fk_orgId) {
this.org = fk_orgId;
}
/**
* @param string
*/
public void setLastName(String string) {
lastName = string;
}
}
Organization:
Code:
public class Organization extends BasePersistentObject {
private Integer orgId;
private String orgName;
private String orgAddress;
private Set employees = new HashSet();
public Organization(){ }
public Organization(Integer id, String name, String add){
this.orgId = id;
this.orgName = name;
this.orgAddress = add;
}
// i also tried inserting table="Employee" under
// @hibenate.set tag
/**
*
* @hibernate.set
* lazy="false"
* inverse="true"
*
* @hibernate.collection-key
* column="fk_orgId"
* @hibernate.collection-one-to-many
* class="hibernate.Employee"
*
* @return all the employees
*
*/
public Set getEmployees() {
return employees;
}
/**
*
* @hibernate.property
* column="orgAddress"
*
* @return
*
*/
public String getOrgAddress() {
return orgAddress;
}
/**
*
* @hibernate.id
* generator-class="native"
* column="orgId"
*
* @return
*/
public Integer getOrgId() {
return orgId;
}
/**
*
* @hibernate.property
* column="orgName"
*
* @return
*/
public String getOrgName() {
return orgName;
}
/**
* @param set
*/
public void setEmployees(Set emps) {
employees = emps;
}
/**
* @param string
*/
public void setOrgAddress(String string) {
orgAddress = string;
}
/**
* @param integer
*/
public void setOrgId(Integer integer) {
orgId = integer;
}
/**
* @param string
*/
public void setOrgName(String string) {
orgName = string;
}
}
My test looks like this. The only thing I changed was to use getHibernateTemplate().load instead of session.load() (because I'm using Spring). It shouldn't matter.
Code:
public void printEmployees(int organizationId) {
Employee emp10 = (Employee)getHibernateTemplate().load(Employee.class, new Integer(10));
System.out.println("Employee name: " + emp10.getLastName() + ", " + emp10.getFirstName());
Organization org10 = emp10.getOrg();
System.out.println("Organization of employee: " + org10.getOrgName());
Organization organization1 = (Organization)getHibernateTemplate().load(Organization.class, new Integer(10));
System.out.println(" organization name: " + organization1.getOrgName());
Set org10Employees = organization1.getEmployees();
Iterator it = org10Employees.iterator();
while (it.hasNext()){
Employee empOrg10 = (Employee)it.next();
System.out.println("Employee is: " + empOrg10.getLastName());
}
}
This is the result of the test. It prints the right employees - Brissett and Dooley
Code:
Hibernate: select employee0_.empId as empId1_, employee0_.firstName as firstName1_, employee0_.fk_orgId as fk_orgId1_, employee0_.lastName as lastName1_, organizati1_.orgId as orgId0_, organizati1_.orgAddress as orgAddress0_, organizati1_.orgName as orgName0_ from Employee employee0_ left outer join Organization organizati1_ on employee0_.fk_orgId=organizati1_.orgId where employee0_.empId=?
Hibernate: select employees0_.empId as empId__, employees0_.fk_orgId as fk_orgId__, employees0_.empId as empId0_, employees0_.firstName as firstName0_, employees0_.fk_orgId as fk_orgId0_, employees0_.lastName as lastName0_ from Employee employees0_ where employees0_.fk_orgId=?
Employee name: Dooley, Rick
Organization of employee: TD Waterhouse
Hibernate: select organizati0_.orgId as orgId0_, organizati0_.orgAddress as orgAddress0_, organizati0_.orgName as orgName0_ from Organization organizati0_ where organizati0_.orgId=?
Hibernate: select employees0_.empId as empId__, employees0_.fk_orgId as fk_orgId__, employees0_.empId as empId0_, employees0_.firstName as firstName0_, employees0_.fk_orgId as fk_orgId0_, employees0_.lastName as lastName0_ from Employee employees0_ where employees0_.fk_orgId=?
organization name: TD Waterhouse
Employee is: Brissett
Employee is: Dooley
The mappings I have left unchanged.
Check the size of org10Employees, then check how many employees in the database have fk_orgid = 10.
Later.[/code]