User和Group是多对多的关系,类和映射文件分别如下:
User类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace UserGroup
{
    public class User
    {        public User()
        {
        }        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }        public string Name
        {
            get { return name; }
            set { name = value; }
        }        public IList Groups
        {
            get { return groups; }
            set { groups = value; }
        }        private int userId;
        private string name;
        private IList groups = new ArrayList();    }
}User.hbm.xml映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="UserGroup.User, UserGroup" table="users">
    <id name="UserId" column="user_id" type="Int32"   unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Name" column= "name" type="String"/>
    <bag name="Groups" table="UserGroups" inverse="true" lazy="false">
      <key column="user_id" />
      <many-to-many column="group_id" class="UserGroup.Group, UserGroup" />
    </bag>
  </class>
</hibernate-mapping>Group类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace UserGroup
{
    public class Group
    {        public Group()
        {
        }        public int GroupId
        {
            get { return groupId; }
            set { groupId = value; }
        }        public string Name
        {
            get { return name; }
            set { name = value; }
        }        public string Description
        {
            get { return description; }
            set { description = value; }
        }        public IList Users
        {
            get { return users; }
            set { users = value; }
        }        private int groupId;
        private string name;
        private string description;
        private IList users = new ArrayList();    }
}Group.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="UserGroup.Group, UserGroup" table="groups">
    <id name="GroupId" column="group_id" type="Int32"   unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Name" column= "name" type="String"/>
    <property name="Description" column= "description"   type="String"/>
    <bag name="Users" table="UserGroups" lazy="false">
      <key column="group_id" />
      <many-to-many column="user_id" class="UserGroup.User, UserGroup" />
    </bag>
  </class>
</hibernate-mapping>数据库:
create database Hibernate
go
use Hibernate
gocreate table Users (user_id int identity primary key, name varchar(100))
gocreate table Groups(group_id int identity  primary key, name varchar(100), description varchar(100))
gocreate table UserGroups (group_id int foreign key references Groups(group_id), 
user_id int foreign key references Users(user_id))
go添加相关数据记录,测试用户所拥有的组,测试代码如下:
            User oUser = (User)session.Load(typeof(User), 1);
            IList ilGroup = oUser.Groups;
            Group[] arrayGroup;
            arrayGroup = (Group[])((ArrayList)ilGroup).ToArray(typeof(Group));
            foreach (Group group in arrayGroup)
            {
                Console.WriteLine(group.Name);            }
运行时出现的提示是:无法将类型为“NHibernate.Collection.Bag”的对象强制转换为类型“System.Collections.ArrayList”。
提示错误代码行是arrayGroup = (Group[])((ArrayList)ilGroup).ToArray(typeof(Group)),请问是什么原因?谢谢!