-->
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: Foreign key generation
PostPosted: Wed Oct 18, 2006 11:01 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hi everybody!

I have a general (stupid?) question about the assosiations weithin hibernate.
I use hibernate with my IDs mostly assigned. Dont know but that might be important for the question.

Say I have to Tables: PERSON and PHONE_NUMBER.
PERSON consits out of an id (PK) and name.
PHONE_NUMBER consits out of a phone number(PK) an a person_Id (PK,FK).

The assoiation is biderectional, so that I have a <set>..<one-to-many...></set> within the PERSON.hbm.xml and a <many-to-one.../> within the PHONE_NUMBER.hbm.xml.

Now here comes the question:
I have the folowing pseudo code snippet
Code:
Person person = new Person();
person.setID(new Integer(1));
person.setName("Foo Bar");
HashSet phoneNumbers = new HashSet();
//Please notice that I don't assign any Person_Id to the phone numbers
phoneNumbers.add(new PhoneNumber("0049977196234"));
phoneNumbers.add(new PhoneNumber("0044978967454"));
phoneNumbers.add(new PhoneNumber("0049782348734"));
person.setPhoneNumbers(phoneNumbers);
...
session.saveOrUpdate(person);
...


I want to create a new Person including phonenumbers and let hibernate make it persistent. I dont want to explitly set the foreign key. I want hibernate to recognize that these phone_numbers belong to that specific person and set the person_Id for me.

Do you what I want to do?
Is it possible at all?
Can I still stick to my <generator class="assigned"/> or do I have to give it up?
Do you need more information from me?


Thank you!
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 11:28 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Stefan,

As far as I know you need to assign the Person object to the proper propertiy of the Phone object. You do not need to worry about the exact value of the ID as long as the object tree is constructed properly in memory. Also you need to have cascade all set on the set of phone numbers.

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 4:06 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Thank you.

I understand why it has to be like you said.

Thanks again,
Stefan


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.