Just to offer a different perspective to Hudson's: I do not agree that our job stops with finding the correct NH tag, nor should we let NH do all the worrying about how things are implemented in the database. I'm all in favour of building a domain model that suits the developers needs AND a data model that satisfies the DBA's and/or the performance requirements of the system. Often as developers we have no control over the database schema at all, be it due to legacy systems or other factors! The beauty of a tool like NH is that it gives you the power to have the best of both worlds, where the same domain model can be mapped to many possible schemas, and vice versa.
That's where the disagreement ends though. He's right that the proposed schema doesn't make a lot of sense for the classes you've shown us. Is there a unique constraint on column idB of table AB? That would make a little more sense in that it would ensure there cannot be multiple A's associated with a particular B. I suspect that there IS a way to get NH to do what you want but I'm not sure how, and whether it will be possible with FNH. You might be better off posting to the NH users list<http://groups.google.com/group/nhusers> and asking about the xml mapping for those tables and classes, if it exists. The readers of that list are better versed in NH's more esoteric mapping features. If you do manage to find some xml that works, we can investigate how to get it done with FNH. On Sat, Jan 23, 2010 at 3:13 AM, Hudson Akridge <[email protected]>wrote: > 1.) Why do you want 3 tables as a result? That's not how your objects are > composed > 2.) HasManyToMany(). But that's not going to work exactly because in order > to have a join table created and persisted correctly, you should have a > reference on both sides to the HasManyToMany. Plus, it'll need to be a > collection reference like a ICollection<A> refObjs; or something like that. > You can technically do it with only one ManyToMany on the B side, but it's > not advised. > 3.) What you're really looking for is a References() mapping (many-to-one > in NH terms). That is how you would make that association. You will end up > with a null if there's no reference, but that's ok because of the next > point. > 4.) As a suggestion, don't worry too much about how your database is > created as a result of your mappings. The point of Domain Driven design is > that it's domain first, Data second. Associate your domain objects > correctly, and there's an NH mapping for you. If you try and do both domain > and data driven design simultaniously, one of the two of them is going to be > competing with the other and creating friction. Your database doesn't have > to be fully normalized to be mapped correctly. In some cases, that's a bad > solution since it'll significantly impact performance. You want to map your > domain to the appropriate NHibernate mapping tags. Let NH worry about how it > implements those in the database. Your job stops when you find the correct > NH tag. In this case, it's many-to-one (References in FNH). > > > On Fri, Jan 22, 2010 at 4:51 AM, Dana Efros <[email protected]>wrote: > >> >> >> Hi. I have to 2 entities: >> >> class A{ >> int id; >> string name;} >> >> class B{ >> int id; >> A refObj; >> string name;} >> >> How can I map this 2 classes so I will have 3 tables: >> table A with 2 columns: id and name >> table B with 2 columns: id and name >> table AB with 2 columns: idA and idB >> >> In the AB table I need to have a row only when refObj will not be >> null.The field refObj from the B entity will have values only >> occasionally and I don't want to have a column in the B table with >> null value for almost all the rows >> >> With a many to many relations I know how, but with a reference ... >> Thanks >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Fluent NHibernate" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<fluent-nhibernate%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/fluent-nhibernate?hl=en. >> >> > > > -- > - Hudson > http://www.bestguesstheory.com > http://twitter.com/HudsonAkridge > > -- > You received this message because you are subscribed to the Google Groups > "Fluent NHibernate" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<fluent-nhibernate%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/fluent-nhibernate?hl=en. > -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
