jramosd wrote:
Mmmmmh, sorry, the actual object is more complicated and I simplified it to send the example. I took away too much code.
Not sure what to tell you.
I copied your mapping files and your Java class files(adding the agency property) into a test project and after inserting some data with the following code
Code:
Session session = getSession();
Transaction tx = session.beginTransaction();
Agency agency1 = new Agency();
agency1.setName("Test Agency 1");
session.save(agency1);
BranchOffice branchOffice1 = new BranchOffice();
branchOffice1.setName("Branch Office 1");
branchOffice1.setAgency(agency1);
session.save(branchOffice1);
BranchOffice branchOffice2 = new BranchOffice();
branchOffice2.setName("Branch Office 2");
branchOffice1.setAgency(agency1);
session.save(branchOffice2);
AgencyOperator agOp1 = new AgencyOperator();
agOp1.setBranchOffice(branchOffice1);
agOp1.setName("Preston");
agOp1.setPassword("password");
session.save(agOp1);
try {
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
I executed a simple query and it seems to work as expected.
Code:
Session session = getSession();
try {
Query q = session.createQuery("from AgencyOperator");
for (Iterator i = q.list().iterator(); i.hasNext();) {
AgencyOperator agOp = (AgencyOperator) i.next();
System.out.print(agOp.getName() + " " + agOp.getBranchOffice().getName());
if (agOp.getBranchOffice().getAgency() == null) {
System.out.println("Why am I null ?");
} else {
System.out.println(agOp.getBranchOffice().getAgency().getName());
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
session.close();
}