多对多关系在数据库也是比较常见的,它通过一个中间表将两个主表关联起来。下面来看看多对多关联在nhibernate的实现,示例是一个用户和组之间的多对多关联。
先来看看用户类的映射信息:
<class name="User, AssemblyName" table="Users">
<id name="UserId" unsaved-value="0" type="int32" column="user_id">
<generator class=identity/>
</id>
<property name="Name" type="String" column="name"/>
<set name="Groups" table="UserGroups" inverse="true">
<key column="user_id"/>
<many-to-many class="Group, AssemblyName" column="group_id"/>
</set>
</class>
在多对多定义中,定义了中间表为UserGroups,此表只有两个字段:user_id和group_id;用于关联Users和Groups表。
用户类的定义:
public class User {
public User() {
}
public int UserId
{
get { return userId; }
set { userId = value; }
}
public int Name
{
get { return name; }
set { name = value; }
}
public IDictionary Groups
{
get { return groups; }
set { groups = value; }
}
private int userId;
private string name;
private IDictionary groups = new Hashtable();
} //class User
这里用一个数据字典IDictionary对角来保存组对象。
再来看看组类的映射信息:
<class name="Group, AssemblyName" table="Groups">
<id name="GroupId" unsaved-value="0" type="int32" column="group_id">
<generator class=identity/>
</id>
<property name="Name" type="String" column="name"/>
<property name="description" type="String" column="description"/>
<set name="Users" table="UserGroups">
<key column="group_id"/>
<many-to-many class="User, AssemblyName" column="user_id"/>
</set>
</class>
这里many-to-many的定义和用户类映射信息中的差不多。
组类的定义:
public class Group {
public Group() {
}
public int GroupId
{
get { return groupId; }
set { groupId = value; }
}
public int Name
{
get { return name; }
set { name = value; }
}
public int Description
{
get { return description; }
set { description = value; }
}
public IDictionary Users
{
get { return users; }
set { users = value; }
}
private int userId;
private string name;
private IDictionary groups = new Hashtable();
} //class User
注意:多对多没有主次之分,保存时的两边都要save。
下面给出部分测试代码。
public TestCreate() {
User user1 = new User();
user1.Name = "test1";
User user2 = new User();
user2.Name = "test2";
Group group1 = new Group();
group1.Name = "group1";
Group group2 = new Group();
group2.Name = "group2";
user1.Groups.Add( group2, group2 );
user2.Groups.Add( group1. group1 );
group1.Users.Add( user2, user2 );
group2.Users.Add( user1, user1 );
ITransactioin trans = null;
try {
trans = session.BeginTransaction();
Session.Save( user1 );
Session.Save( user2 );
Session.Save( group1 );
Session.Save( group2 );
trans.Commit();
}
catch ( Exception e ) {
if ( trans != null ) trans.Rollback();
throw e;
}
finally {
session.Close();
}
}
以上测试代码中session的相关操作请查看相关文档。