You've got it. The DB needs a mapping table with two FKs. But your domain does 
not need to model that table as an entity. Conceptually, it's not an entity. 
Once NH knows about it it handles the rest giving you a nice clean m2m 
abstraction.


On Thursday, September 29, 2011 at 11:11 PM, Joe Brockhaus wrote:

> Hi Tim,
> 
> I've had some trouble wrapping my head around many2many (and have
> worked around it hackishly using the tagpost entity metaphor) ..
> 
> If I understand you correctly, the above m2m mapping means that i have
> a table PostTag that links posts w/ tags, which /does not need/ an
> entity w/ mapping to the m2m link table representing it?
> 
> I'm curious how this affects adding to the tags collection (updating
> the collection/inserting new tags)? Will adding a new, transient, tag
> to a post automatically perform an insert of that new tag to the tag
> table and also a new row in the posttag table? What if my posttag
> table has a pk, generated by a sequence?
> 
> On 9/29/11, Tim Scott <[email protected] 
> (mailto:[email protected])> wrote:
> > I should add that there's probably a way to reduce much of this override
> > code by specifying conventions for many-to-many auto mapping. But this is
> > how I got many-to-many auto mapping to work for me.
> > 
> > 
> > On Thursday, September 29, 2011 at 5:35 PM, Tim Scott wrote:
> > 
> > > You DO NOT want to have a PostTag entity to worry about. NHibernate can
> > > handle this as a many-to-many association.
> > > 
> > > If you were to add a Posts collection to Tag I'm guessing that
> > > auto-mapping would make it all just work. However, you probably don't want
> > > that. You want something like this:
> > > 
> > > public class PostMappingOverride : IAutoMappingOverride<Post>
> > >  {
> > > public void Override(AutoMapping<Post> mapping)
> > > {
> > > mapping.HasManyToMany(x => x.Tags)
> > >  .Access.CamelCaseField()
> > >  .ChildKeyColumn("TagId")
> > >  .ParentKeyColumn("PostId")
> > >  .Cascade.None()
> > >  .Table("PostTag");
> > >  }
> > > 
> > > Tim
> > > 
> > > 
> > > On Tuesday, September 27, 2011 at 6:15 PM, Dando wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm new to FNH, and I'm currently developing a project with Sharp
> > > > architecture. I'm new to ORMs, being used to a more direct hand in
> > > > sql.
> > > > 
> > > > Here were my original classes:
> > > > 
> > > >  class Post : Entity
> > > >  {
> > > >  string name
> > > >  string content
> > > >  List<Tag>
> > > >  }
> > > > 
> > > >  class Tag : Entity
> > > >  {
> > > >  string name
> > > >  }
> > > > 
> > > > I let Automapper do it's stuff, but it was creating a foreign key in
> > > > the Tag table back to Post, which I didn't want, as I wanted Tag to be
> > > > it's own entity. I was expecting a join table of PostId, TagID to be
> > > > created (inferred) upon schema generation.
> > > > 
> > > > So, after some reading, I added the join table to my classes section:
> > > > 
> > > >  class PostTag : Entity
> > > >  {
> > > >  Post post
> > > >  Tag tag
> > > >  }
> > > > 
> > > > 
> > > > and I changed the Post table to the following:
> > > > 
> > > >  class Post : ENtity
> > > >  {
> > > >  string name
> > > >  IList <PostTag>
> > > >  }
> > > > 
> > > > Tag class stays the same. Now the generated schema creates the join
> > > > table schema as well. Schemas now look as follows:
> > > > 
> > > > Post{id, name, content}
> > > > 
> > > > Tag{id, name}
> > > > 
> > > > PostTag{id, PostFK}
> > > > 
> > > > 
> > > > 
> > > > For PostTag, the TagFK isn't there.
> > > > 
> > > > Is there a way of doing this with automapper? Also, I don't think
> > > > there should be a normal Id column in this table. It should
> > > > presumably be a table with a composite PK of PostId and TagID?
> > > > 
> > > > I don't want to be putting IList <PostTag> onto my Tag class, as the
> > > > Tag class should not have a reference going back to the Post object.
> > > > 
> > > > Would anybody be able to help here? Does this need an override, and
> > > > if so, how do I go about this?
> > > > 
> > > > I'm sorry if this is an EXTREMELY noobish question!
> > > > 
> > > > --
> > > > 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] 
> > > > (mailto:[email protected])
> > > > (mailto:[email protected]).
> > > > To unsubscribe from this group, send email to
> > > > [email protected] 
> > > > (mailto:[email protected])
> > > > (mailto:[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] 
> > (mailto:[email protected]).
> > To unsubscribe from this group, send email to
> > [email protected] 
> > (mailto:[email protected]).
> > For more options, visit this group at
> > http://groups.google.com/group/fluent-nhibernate?hl=en.
> 
> -- 
> Sent from my mobile device
> 
> ------
> Joe Brockhaus
> [email protected] (mailto:[email protected])
> ------------
> 
> -- 
> 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] 
> (mailto:[email protected]).
> To unsubscribe from this group, send email to 
> [email protected] 
> (mailto:[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.

Reply via email to