Mapping a tree/hierarchy with self referencing parent child relationship

237 views
Skip to first unread message

Chris Nicola

unread,
Dec 30, 2009, 6:33:20 PM12/30/09
to Fluent NHibernate
I have a pretty simple self referencing parent-child tree
relationship, however it is important that this is a many-to-one
relationship and *not* a many-to-many.

If I create my entity like this:

public class Comment : EntityWithTypedId<Guid> {
public virtual string Author { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string WebAddress { get; set; }
public virtual string Content { get; set; }
public virtual DateTime DatePosted { get; set; }
public virtual Post Post { get; set; }
public virtual Comment Parent { get; set; }
public virtual IList<Comment> Children { get; set; }
}

fluent assumes this is a many-to-many relationship (which I can
understand). In order to work around this I use an override:

public void Override(AutoMapping<Comment> mapping) {
mapping.References<Comment>(x => x.Parent).Column("ParentFk");
mapping.HasMany<Comment>(x => x.Children).Inverse().KeyColumn
("ParentFk");
}

but I was curious if this can be done at all using conventions. As I
rule I never create many-to-many mappings without an intermediate
mapping class so I would never map manytomany this way. What I would
like is for fluent to map this as many-to-one.

I have tried everything I can think of but I can't make the
conventions do this.

Thanks,
Chris

Hudson Akridge

unread,
Dec 30, 2009, 6:55:38 PM12/30/09
to fluent-n...@googlegroups.com
I don't believe a convention would be able to assist you at this point, because by then, it would be too late. A convention should be applied after the mapping type has been determined. So by the time your convention would fire, the hbm part type would already have been chosen for you, preventing you from being able to change it.

Overrides would be the way to go, unless you wanted to use Fluent Mappings.

However, I rarely use Automap, so someone might be able to jump in and correct me if I'm wrong.


--

You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
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
Reply all
Reply to author
Forward
0 new messages