I am having trouble mapping the many to many relationship! I have a Users table, Roles table and UserRoles table. A user can have many roles. The Roles table just store the name of the roles like Admin, Editor etc. The Userroles table stores the relationship of user with the roles.
When I save the user the user information is saved but the Role Information is not saved.
Here is my mappings:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
auto-import="true" assembly="EStudy.Business" namespace="EStudy.Business.Entities">
<class name="EStudy.Business.Entities.User, EStudy.Business" lazy="false" table="Users">
<id name="Id" access="property" column="UserId">
<generator class="native" />
</id>
<property name="UserName" access="property" column="UserName" />
<property name="Password" access="property" column="Password" />
<property name="FirstName" access="property" column="FirstName"/>
<property name="LastName" access="property" column="LastName"/>
<property name="DateCreated" access="property" column="DateCreated" generated="insert" type="datetime" />
<property name="DateModified" access="property" generated="always" column="DateModified" type="datetime" />
<bag name="Roles" table="UserRoles" cascade="all" lazy="true" access="nosetter.camelcase-underscore" >
<key column="UserId"/>
<many-to-many class="Role" column="RoleId" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
auto-import="true" assembly="EStudy.Business" namespace="EStudy.Business.Entities">
<class name="EStudy.Business.Entities.Role, EStudy.Business" lazy="false" table="Roles">
<id name="Id" access="property" column="RoleId">
<generator class="native" />
</id>
<property name="RoleName" access="property" column="RoleName"/>
<bag name="Users" table="UserRoles" access="nosetter.camelcase-underscore" cascade="all" inverse="true">
<key column="RoleId"/>
<many-to-many class="EStudy.Business.Entities.User, EStudy.Business" column="UserId"/>
</bag>
</class>
</hibernate-mapping>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace EStudy.Business.Entities
{
public class User
{
public User() { }
public void AddRole(RoleType roleType)
{
AddRole(new Role() { RoleName = roleType.ToString() } );
}
private IList<Role> _roles = new List<Role>();
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual DateTime DateModified { get; set; }
public virtual IList<Role> Roles
{
get { return new ReadOnlyCollection<Role>(_roles); }
}
protected virtual void AddRole(Role role)
{
_roles.Add(role);
role.AddUser(this);
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace EStudy.Business.Entities
{
public class Teacher : User
{
public Teacher()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace EStudy.Business.Entities
{
public enum RoleType
{
Teacher,
Student,
Admin
} ;
public class Role
{
private IList<User> _users = new List<User>();
public virtual int Id { get; set; }
public virtual string RoleName { get; set; }
public virtual RoleType RoleType { get; set;}
public void AddUser(User user)
{
_users.Add(user);
}
public virtual IList<User> Users
{
get { return _users; }
}
}
}
The above mapping file is called User.hbm.xml and it contains the mapping for User and Role and their relationship with the Userrole table.