在多对多定义中,定义了中间表为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对角来保存组对象。再来看看组类的映射信息:
这里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的相关操作请查看相关文档。