I reread the documentation. I guess I should have explained that I had read it but wasnt really clear on how to implement what the doc had shown.
KPixel, You commented: "There is a special requirement (on the id) when trying to use "primary key associations"..."
From the doc I took it to understand the "primary key association" was to be implemented if I wanted to have my Phone PK and my Person PK share the same key. I want to store the information in a seperate column (cellphone) a reference to the Phone PK.
For this reason I implemented the "unique foreign key associations". Is this the correct one to use?
I have added the following to my Person.hbm.xml
Code:
<many-to-one name="CellPhone" class="SS.Phone, SS" column="CellPhone" unique="true" />
This will now allow me to store the pk of the phone in the person.
A few more questions...
1. Do you have to save the instance of cellphone manually before you save the person? In my experimentation it seems that you do...I get an exception about an unsaved transient if I dont. I would think the save would take care of the whole save since I set the instance of the phone to the users cell phone. I guess Im wondering if therre is a better / cleaner way to do this.
This is how im saving things currently
Code:
Person newUser = new Person();
newUser.FirstName = "JosephFirst";
newUser.LastName = "SmithLast";
Phone newCellPhone = new Phone();
newCellPhone.AreaCode = "410";
newUser.CellPhone = newCellPhone; //Would'nt this add it to the 'dirty set'?
session.Save(newCellPhone); //I have to save the cellphone first
session.Save(newUser); //Why doesnt this persist the cell at the same time?
2. Whenever I attempt to add the one-to-one relationship to the phone (to make it bidirectional, according to the doc). Using this:
Code:
<one-to-one name="PhoneIdAsOtherProperty" class="SS.Person, SS" property-ref="CellPhone"/>
If I return phone id through antother method. I get an exception:
Code:
[ArgumentException: There is a problem with your mappings. You are probably trying to map a System.ValueType to a <class> which NHibernate does not allow or you are incorrectly using the IDictionary that is mapped to a <set>.
A ValueType can not be used with IdentityKey. The thread at google has a good description about what happens with boxing and unboxing ValueTypes and why they can not be used as an IdentityKey: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=bds2rm%24ruc%241%40charly.heeg.de&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26q%3DSystem.Runtime.CompilerServices.RuntimeHelpers.GetHashCode%26sa%3DN%26tab%3Dwg
Parameter name: key]
As you can see im lost. what should the appropiate property (name) be for the one-to-one mapping?
gekannt, I agree, with your comments. This probally wasnt the best example but I do feel there are situations that would require this approach.
Thanks again for you input
Sean