NHibernate映射多对多关系

时间:2009-08-18 18:45:12

标签: nhibernate

我无法映射多对多的关系!我有一个Users表,Roles表和UserRoles表。用户可以拥有许多角色。 Roles表只存储Admin,Editor等角色的名称.Userroles表存储用户与角色的关系。

保存用户时,会保存用户信息,但不保存角色信息。

这是我的映射:

       <?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,它包含User和Role的映射以及它们与Userrole表的关系。

2 个答案:

答案 0 :(得分:1)

你错过了cascade =“all-delete-orphan”。查看NH文档。

答案 1 :(得分:1)

解决方案是在事务中进行插入。