views:

31

answers:

2

Lets say i have two entities;

QuestionAnswer(Id, AnswerValue)
Note(Id, QuestionAnswer_Id, NoteValue)

How would I map this in Fluent Nhibernate? I know that there is a HasOne mapping but this is for a 1 to 1 unless im mistaken?
I could also map it as a 1 to M but would require a List<Note> as a navigation property on my QuestionAnswer entity which again doesnt make much sence for a 1 or 0 to me.

Thanks, Kohan

+1  A: 

Use one-to-one association, and set the Note to null if it is not available.

Also, see Hibernate one to zero or one mapping.

Sjoerd
Are you saying i should map a 1 to many from QuestionAnswer to Note and a One to One from Note to QuestionAnswer. This will, as i mentioned, require a List<Note> on my QuestionAnswer entity that will only ever have 1 or 0 objects in it. Seems odd to me; to have a list that will never have more than 1 item.
Kohan
Why would it? many-to-one and one-to-one (References and HasOne in FNH, respectively) don't use collections.
James Gregory
Because I said “OneToMany (HasMany)” which does use a collection. Sorry but it looks like my understanding is off. I could visualise it working with a HasMany as it would work with 0 or 1 (or more, due to being a collection). But what you are saying is that I actually need References<Note>(x => x.Note).NotFound.Ignore(); for QuestionAnswer’s mapping and HasOne<QuestionAnswer>(x => x.QuestionAnswer); for my note's mapping?
Kohan
one-to-one requires that the tables have the same primary key, doesn't it?
Jamie Ide
+1  A: 

I've struggled with this also. What I ended up doing is mapping a private collection and using public properties to control the number of elements in the collection. Basically I treat it as a 1..n collection instead of a 1..* collection where n = 1.

Jamie Ide