Fluent NHibernate auto mapping - how to create a many-to-many relationship table?

1.3k Views Asked by At

just wondered if anyone know if there is a way to crate a Many-to-Many relationship table automatically using some attribute? without creating the table, or mapping a class to become that relation table.

If i add the attribute [ManyToMany(3,Class="DeploymentListUsers")] i get an error that this class isn't mapped to the DB.

NHibernate.MappingException: An association from the table Users refers to an unmapped class:

I don't want to define the class myself, nor creating the table in the DB before hand.

it's seems possible in JAVA, does fluent NHibernate is a fully implemented version of hibernate and JPA annotations?

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=19mappingmanytomanyrelationships

Thanks, Itay

3

There are 3 best solutions below

0
On

If you specify a class, you must make sure it exists too. NHibernate will not create a class for you. If however, your many-to-many table only contains a foreign key to your deployments table and a foreign key to your users table, you won't need such a class. You will only need a separate many-to-many mapping class if you want to store additional facts about the relation.

As far as I can tell, in your situation you just need to create the many-to-many table (you could use NHibernate's SchemaExport() to generate the CREATE TABLE statement so there's some automation there) and tell NHibernate how to map to it. Unlike you, I don't use mapping attributes so I don't know how to do map using them but it is probably very simple.

0
On

NHibernate now supports JPA (called NPersistence or NPA for the .NET platform) including JPA annotations. It is the best way to specify relations and have your code portable for other vendors as well. check it out: www.npersistence.com

0
On

My answer to a similar question might help you out: https://stackoverflow.com/a/12198533/185200

I ended up extending the many-to-many automapping step to support properties decorated with a custom attribute.