我無法映射多對多關係!我有一個Users表,Roles表和UserRoles表。用戶可以有許多角色。角色表只存儲像Admin,Editor等角色的名稱。Userroles表存儲用戶與角色的關係。NHibernate映射多對多關係
當我保存用戶時,會保存用戶信息,但不保存角色信息。
這裏是我的映射:
<?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; }
}
}
}
上述映射文件名爲User.hbm.xml,它包含了用戶和角色及其與UserRole的表關係的映射。
問題是爲什麼它需要一個交易? – azamsharp 2009-08-19 11:43:22