Hi!
I have been banging my head against this for two days now...
NHibernate throws "PropertyNotFoundException" and noting gets persisted to the db. What is the proper way of doing this? Please post a working solution for loading/saving many-to-many relations using a collection in the two domain objects.
Two classes:
Code:
class User {
int id;
string name;
IList roles;
}
class Role {
int id;
string name
IList users;
}
The database contains three tables: User, Role and UserRole for the many-to-many relationship..
My mappings:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="FirstNHibernate.Domain.User, FirstNHibernate" table="User">
<id name="ID" column="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="Name" type="String" length="50"/>
<bag name="Roles" table="UserRole" lazy="false">
<key column="UserID" />
<many-to-many column="RoleID" class="FirstNHibernate.Domain.Role, FirstNHibernate" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="FirstNHibernate.Domain.Role, FirstNHibernate" table="Role">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column= "Name" type="String" length="50"/>
<bag name="Users" table="UserRole" inverse="true" lazy="true">
<key column="RoleID" />
<many-to-many column="UserID" class="FirstNHibernate.Domain.User, FirstNHibernate" />
</bag>
</class>
</hibernate-mapping>
Regards
/Oscar