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