nhibernateshared-primary-key

NHibernate primary key is a 1-1 mapping?


Maybe I have just designed this incorrectly, and if so I am more than happy to bow out and loot at it another way...

I currently have 2 tables like this, which have a shared primary key:

person - id (PK), name, created_date, ...

person_details - person_id (PK), age, height, ...

Now the person gets created and a person can only have one set of details. So initially I thought that when mapping the person_details I would have the PK as being assigned, as it would be generated by the person.

Now im just a bit confused as to how I can create the first instance of this data in the database, as when I create a Person model, which contains an instance of PersonDetails model, neither will have an Id at this point, as they haven't been created. So how do I tell the PersonDetails model to take its Id from the Parent Person model when creating?

Am I just going mad or is there a simple mapping I need to do in the Person model to tell it to infer the PersonDetails Id when inserting from its own Id? I end up needing the Person_Id on the PersonDetails as they are updated externally to the person by Ajax if a change will be made after initial creation...


Solution

  • If the tables share a PK (which I assume is originated in person), then you have either a one-to-one, where a Person has a Detail, or a join, where a Person has Age, Height, etc and they are mapped into a different table.

    Just read both chapters, they have examples.