0

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...

Grofit
  • 17,693
  • 24
  • 96
  • 176
  • After thinking about this a bit more, it seems that one to one mapping may not be the best way to do this, even though in the database they are a one to one mapping... so is there a better way to structure this? Ultimately I need to be able to update the details (and some other one to one style relationships) independently via ajax, so they need to have some relationship so I can save them individually without having to save the entire person, unless this is the only way to do it... – Grofit Mar 06 '11 at 15:34

1 Answers1

1

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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Hi, read those already, but the problem for me is that a Person model contains ALOT of information, and if I were to have a Person with a PersonDetails model (and other models) then that model has a reference back to the parent Person I would be passing around a hell of alot of data on postbacks, as i currently use MVC. This is why i was wanting to have a PersonId opposed to a Person within the models. I know they would be lazy loaded so its not like im actually going to have tons of data floating around, but im pretty sure the mvc postback would have tons of null data... will have to check – Grofit Mar 06 '11 at 15:15
  • The problem is trying to modify your *Domain* model for HTTP posting. Create a *Presentation* model instead. – Diego Mijelshon Mar 06 '11 at 16:07
  • I was originally going to do that and write a mapper to go to and from, but I thought there was no point if I could will NHibernate to act as I wanted it to... – Grofit Mar 06 '11 at 17:40