These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to assigned auto-generated PK to its FK - Please suggest
PostPosted: Thu Nov 02, 2006 4:03 pm 
Newbie

Joined: Thu Aug 03, 2006 11:18 pm
Posts: 5
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 4:33 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
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.

Code:

<many-to-one
        name="hospital"
        class="package.Hospital"
        not-null="true"
    >
        <column name="foreignkey_column_name" />
    </many-to-one>


patient.setHospital(hospital);

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 5:11 pm 
Newbie

Joined: Thu Aug 03, 2006 11:18 pm
Posts: 5
Thanks for being a great group of people.

Your post was really very helpful. Now, its all working fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.