Fluent NHibernate Automapping for Unidirectional ManyToMany

235 views
Skip to first unread message

SamerX

unread,
Jan 22, 2012, 3:19:49 AM1/22/12
to Fluent NHibernate
I am using Fluent NHibernate Automapping, and I faced a situation
where I have a Unidirectional ManyToMany ("A" has a list of "B" and
"B" knows nothing about "B") relationship, and this relationship is
the default behaviour for me since it mostly used when having a
relationship with lookups.

So after doing the standard behavior of mapping for ManyToMany
relationship and using the ManyToManyTableNameConvention class I found
out that FluentNhibernate Automapping considers the relation I
specified above as a HasMany (OneToMany) relationship, although there
is no reference in the other side to the first side, I wanted to
change this behavior and the only way was to create my own
HasManyToManyStep and inject it within the Custom
DefaultAutomappingConfiguration class.

In the CustomHasManyToManyStep class I specified that if its a normal
Bidirectional Relationship then proceed as normal, while if its not
then see if its a Unidirectional Relationship, if its then the only
trick is to specify the correct parameter for the
GetParentSideInManyToMany method, and it was actually easy.

What I wonder is why not to have that as an option in Fluent
Nhibernate configuration, I believe such a relation is extremely
important and widely used, I would suggest to add it to the framework
with a simple behavior switch for How to deal with such a relation:
HasMany or Unidirectional ManyToMany.

I have the code for it if you want me to share it I can do that.

SamerX

unread,
Jan 22, 2012, 3:18:00 AM1/22/12
to Fluent NHibernate

Tim Scott

unread,
Jan 27, 2012, 11:02:46 AM1/27/12
to fluent-n...@googlegroups.com
I've never heard of "unidirectional many-to-many."  "A has a list of B and B knows nothing of A" is the normal definition of "one-to-many."

You mention CustomerHasManyToManyStep class. To model many-to-many in NHibernate you should never need any relationship classes.  Simply have a collection of A in B and a collection of B in A.  Auto-mapping should work it out. You will need a relationship table with a specific conventional name.  You can change that with a mapping override or globally using ManyToManyTableNameConvention.

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

SamerX

unread,
Jan 27, 2012, 3:49:03 PM1/27/12
to Fluent NHibernate
The main idea is when you dont have a list of B in A or any other
reference, having it this way its a many to many relationship and you
have two options here: either to be considered as a HasMany or a
ManyToMany .. while fluent nhibernate always assumes that such a
relation is a HasMany the truth is this is not always correct ..
actually the default behaviour in my openion is to be considered as a
Unidirectional ManyToMany .. and that you will be using a conjunction
table in the database .. look it this way .. B is in a class that you
have no control of and its only referenced.
> > To post to this group, send email to fluent-n...@googlegroups.com (mailto:fluent-n...@googlegroups.com).
> > To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com (mailto:fluent-nhibern...@googlegroups.com).
Reply all
Reply to author
Forward
0 new messages