don't worry about that for now - go with what should work first...
Get rid of all occasions where you are using the ID's directly and make sure it is completely related by objects -- ie:
Code:
employee.setLocationId(x);
The only place you're dealing with IDs and FKs are with each entity bjects as you setup it's own ID, and the FK.
Did you notice how the sample has a "class" defined that represents the ID's on each of the entities?
It should look like similar to this when you're done
Off the top of my head without testing for typos, this should work.
Code:
Company
{
CompanyPK coPK=null;
String name="";
Set emps=null;
getters/settters for pk and name and set.
}
Code:
CompanyPK
{
Long companyId=null;
Long locationId=null;
//getters/setters, hashCode and equals
}
Code:
Employee
{
EmployeePK empPK=null;
String empno="";
Company co=null;
//getters and setters for empPK and empno, and co
}
Code:
EmployeePK
{
Long empId=null;
Long companyId=null;
Long locationId=null;
//getters/setters, hashCode and equals
}
Then your mapping would be:
Code:
<class name="Company">
<composite-id name="coPK"
class="CompanyPK">
<key-property name="companyId"/>
<key-property name="locationId"/>
</composite-id>
<property name="name">
<set name="emps"
fetch="join"
lazy="false"
inverse="true"
cascade="save-update">
<key>
<column name="companyId"/>
<column name="locationId"/>
</key>
<one-to-many class="Employee"/>
</set>
</class>
Code:
<class name="Employee">
<composite-id name="empPK"
class="EmployeePK">
<key-property name="companyId" />
<key-property name="locationId"/>
<key-property name="empId"/>
</composite-id>
<property name="empno"/>
<many-to-one name="co">
<column name="companyId"/>
<column name="locationId"/>
</many-to-one>
</class>
Then in your java code do something like:
Code:
Company co=new Company();
CompanyPK coPK=new CompanyPK ();
coPK.setLocationId(234);
coPK.setCompanyId(444);
co.setCoPK(coPK):
co.setName("bbb");
co.setEmps(new HashSet());
Employee emp1=new Employee();
EmployeePK empPK=new EmployeePK();
empPK.setLocationId(234);
empPK.setCompanyId(444);
empPK.setEmpId(1);
emp1.setEmpPK(empPK);
emp1.setCo(co);
co.getEmps().add(emp1);
tx=sees.beginTransaction();
sess.saveOrUpdate(co);
tx.commit();
//both company and it's one emp are saved to database at this point.