NHibernate 3.2 map a many to many by convention

461 views
Skip to first unread message

Action Jackson

unread,
Aug 21, 2011, 4:59:01 AM8/21/11
to nhusers
I apologize if this has been asked before, but I can't find the
answer. I'm trying to map a many-to-many relationship using
NHibernate 3.2 ConventionModelMapper. I'd like to get the same
results that I would get if using ConfOrm's
CoolTablesAndColumnsNamingPack. For example,

class Role
{
public virtual ISet<User> Users { get; set; }
}

class User
{
public virtual ISet<Role> Roles { get; set; }
}

Using ConfOrm, I could use CoolTablesAndColumnsNamingPack
(specifically, the ManyToManyInCollectionTableApplier). By doing so,
I would automatically get a UsersToRoles table which serves as the
association table for the many to many relationship. However, using
mappings by convention, I can't see how to get the X_to_Y association
table automatically. Right now, what's happening is the two tables
have foreign keys to each other which I don't want. For example, the
Users table has a "role_key" column which is not good.

Would the IsManyToMany method of ConventionModelMapper do the trick?
If so, does someone have a working example of using this method to get
the association table?

Fabio Maulo

unread,
Aug 29, 2011, 11:06:03 AM8/29/11
to nhu...@googlegroups.com
LOL!!!
To transform NH's 3.2.0 sexy-mapping to ConfORM you have to take each PatternApplier and transform it to the delegate (event handler) used in NH... Then you have to implements the IModelInspector taking the code from here

Note: 
The ConventionModelMapper is just a "custom" implementation of ModelMapper and both uses an embedded implementation of IModelInspector.

The ConfORM's chunk ported in NH is a represents a "simplification"of ConfORM mostly because I'm pretty sure that 80% of users will use the Conformist-mapping.

If you really want use convention/patterns based mapping then stay in ConfORM.

Markus Zywitza

unread,
Sep 7, 2011, 8:11:05 AM9/7/11
to nhu...@googlegroups.com
I solved it this way (I use slightly different conventions here):
 
With this code
 
mapper.ManyToMany<User,Role>(e=>e.Roles, e=>e.Users);
 
I get the following DDL:
 
create table User_Roles (
 User_key UNIQUEIDENTIFIER not null,
 Roles_key UNIQUEIDENTIFIER not null,
 primary key (User_key, Roles_key)
)
 
Using this method, I can even use multiple M:N relations between two entities, since the table name refers only to the class and property controlling the relation. For example
 
mapper.ManyToMany<User,Role>(e=>e.DeniedRoles);
 
creates a second relationtable between User and Role:
 
create table User_DeniedRoles (
 User_key UNIQUEIDENTIFIER not null,
 DeniedRoles_key UNIQUEIDENTIFIER not null,
 primary key (User_key, DeniedRoles_key)
)
 
-Markus

Billy McCafferty

unread,
Sep 21, 2011, 3:19:14 PM9/21/11
to nhusers
Fabio,

As time goes on, do you intend to port more of ConfORM to NHibernate?
I've been trying out the convention support built in to NHibernate 3.2
and it's working great. In fact, so far, the ONLY convention I
haven't been able to do is many-to-many...all without ConfORM. So do
you foresee adding additional support to NHibernate so that this will
be supported "out of the box" in the future, without ConfORM?

Thanks!
Billy McCafferty


On Aug 29, 8:06 am, Fabio Maulo <fabioma...@gmail.com> wrote:
> LOL!!!
> To transform NH's 3.2.0 sexy-mapping to ConfORM you have to take each
> PatternApplier and transform it to the delegate (event handler) used in
> NH... Then you have to implements the IModelInspector taking the code from
> herehttp://code.google.com/p/codeconform/source/browse/ConfOrm/ConfOrm/Ob...

Shakirov Ruslan

unread,
Oct 9, 2011, 4:56:27 PM10/9/11
to nhu...@googlegroups.com
"I solved it this way (I use slightly different conventions here):
https://gist.github.com/1200323"
 
I got this error:
NHibernate.HibernateException: The specified table already exists

Fabio Maulo

unread,
Oct 13, 2011, 7:25:44 AM10/13/11
to nhu...@googlegroups.com
Which was the problem with many-to-many ?
ConfORM does not have a convention about many-to-many just because, after analyze some prjs, I have realized that it does not exists.
Even in presence of a bidirectional collection you can be sure that the relation is a many-to-many; in ConfORM you have to declare it explicitly in the ObjectRelationalMapper:
orm.ManyToMany<User,Role>();

Kieran Senior

unread,
Dec 1, 2011, 4:12:43 AM12/1/11
to nhu...@googlegroups.com
Is this going to be included in subsequent revisions of NH3? I've actually been bashing my head against a wall for the last 2 days trying to figure this out and lo', this extension method worked a treat.
Reply all
Reply to author
Forward
0 new messages