How to assigned auto-generated PK to its FK - Please suggest
Manju Singh
Greenhorn
Joined: Feb 23, 2005
Posts: 27
posted
0
I have 2 table HOSPITAL and PATIENT with one-to-many relation. hospital_id is PK on HOSPITAL which is auto-increment and same hospital_id is FK on PATIENT. So, while inserting hospital object I want the auto-generated hospital_id from HOSPITAL should be inserted into PATIENT into one save.
I have defined one-to-many relation in both mapping file.
I am populating patient object as patient.setHospitalID(hospital.getHospitalID()); // I am not sure whether this is correct way to do, 'coz its auto increment
But, its inserting zero and I beleieve call to hospital.getHospitalID() will not give auto-increment number because it will be generated by DB at the time of insert.
This is URGENT, please let me know how to assigned same auto-generated PK to its FK
Scott Johnson
Ranch Hand
Joined: Aug 24, 2005
Posts: 518
posted
0
If I remember correctly, you don't need to set the hospital id in the child. Hibernate will do that for you.
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
you do not need to set the hospital ID but the Hospital Object to Patient.
pascal
Manju Singh
Greenhorn
Joined: Feb 23, 2005
Posts: 27
posted
0
Thanks for your reply. I posted same question to hibernate forum and got the reply as was same some what pascal replied.
Reply from Hibernate Forum ------------------------------------------------------------------ In your patient you'll want a many-to-one relationship back to hospital, and then before you save, set the hospital object in the patient.