代码改变世界

nhibernate学习之三级联(Ternary Associations)篇

2008-02-21 15:18  乱世文章  阅读(254)  评论(0编辑  收藏  举报
1) 学习目标
通过进一步学习Nhibernate基础知识,掌握用Nhiberate实现对级联的支持,通过一个简单的用户角色权限系统来体验nhibernate对级联的强大支持。
 
2)开发环境和必要准备 
  开发环境为:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
  必要准备:学习前三篇nhibernate学习系列Nhibernate学习之起步篇-1  ,Nhibernate学习起步之many-to-one篇 ,Nhibernate学习之many-to-many篇

3)示例
  业务需求:实现一个用户角色权限系统,一个用户只有一个角色,一个角色下有多个用户,一个角色下有多个权限,一个权限也对应多个角色
                      要求: (1).创建一个角色 (2)在该角色上创建两个个用户3)创建两个权限4)指定该角色上的权限列表5)获得一个用户的权限列表
  首先看关系数据库关系图:
  
4)实现步骤:
  1.User.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace NhibernateSample1
{
    
public class User
    
{
        
private int _id;
        
private string _name;
        
private string _pwd;
        
private Role _role;
        
/// <summary>
        
/// 编号
        
/// </summary>

        public virtual int Id
        
{
            
get
            
{
                
return _id;
            }

            
set
            
{
                _id 
= value;
            }

        }


        
/// <summary>
        
/// 名称
        
/// </summary>

        public virtual string Name
        
{
            
get
            
{
                
return _name;
            }

            
set
            
{
                _name 
= value;
            }

        }


        
/// <summary>
        
/// 密码
        
/// </summary>

        public virtual string Pwd
        
{
            
get
            
{
                
return _pwd;
            }

            
set
            
{
                _pwd 
= value;
            }

        }

        
public virtual Role Role
        
{
            
get
            
{
                
return _role;
            }

            
set
            
{
                _role 
= value;
            }

        }

    }

}

 User.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name="NhibernateSample1.User,NhibernateSample1" table="Users" lazy="false">
    
<id name="Id" column="Id" unsaved-value="0">
      
<generator class="native" />
    
</id>
    
<property name="Name" column="Name" type="string" length="64" not-null="true" unique="true"></property>
    
<property name="Pwd"  column="Pwd"  type="string" length="64" not-null="true"></property>
    
<many-to-one name="Role"  class="NhibernateSample1.Role,NhibernateSample1" column="RoleID"></many-to-one>
   
</class>
</hibernate-mapping>
2.Role.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace NhibernateSample1
{
    
public class Role
    
{
        
int _roleID;
        
string _roleName;
        IList _list 
= new  ArrayList();
        IList _permissionList 
= new ArrayList();
        
public virtual IList PermissionList
        
{
            
get
            
{
                
return _permissionList;
            }

            
set
            
{
                _permissionList 
= value;
            }

        }

        
public virtual int RoleID
        
{
            
get
            
{
                
return _roleID;
            }

            
set
            
{
                _roleID 
= value;
            }

        }

        
public virtual IList UserList
        
{
            
get
            
{
                
return _list;
            }

            
set
            
{
                _list 
= value;
            }

        }

        
public virtual string RoleName
        
{
            
get
            
{
                
return _roleName;
            }

            
set
            
{
                _roleName 
= value;
            }

        }

    }

}

Role.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name="NhibernateSample1.Role,NhibernateSample1" table="Roles" lazy="false">
    
<id name="RoleID" column="RoleID" unsaved-value="0">
      
<generator class="native" />
    
</id>
    
<property name="RoleName"  column="RoleName"  type="string" length="64" not-null="true"></property>
    
<bag name="PermissionList" table="Role_Permissions" inverse="true" lazy="false" cascade="all">
      
<key column="RoleID"/>
      
<many-to-many class="NhibernateSample1.Permission,NhibernateSample1" column="PermissionID"></many-to-many>
    
</bag>
    
<bag name="UserList" table="Users" inverse="true" lazy="false" cascade="all">
      
<key column="RoleID"/>
      
<one-to-many class="NhibernateSample1.User,NhibernateSample1"></one-to-many>
    
</bag>
  
</class>
</hibernate-mapping>
3.Permission.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace NhibernateSample1
{
    
public class Permission
    
{
        
int _permissionID;
        
string _permissionName;
        IList _roleList 
= new ArrayList();
        
public virtual int PermissionID
        
{
            
get
            
{
                
return _permissionID;
            }

            
set
            
{
                _permissionID 
= value;
            }

        }

        
public virtual string PermissionName
        
{
            
get
            
{
                
return _permissionName;
            }

            
set
            
{
                _permissionName
=value;
            }

        }

        
public virtual IList RoleList
        
{
            
get
            
{
                
return _roleList;
            }

            
set
            
{
                _roleList 
= value;
            }

        }

    }

}

Permission.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name="NhibernateSample1.Permission,NhibernateSample1" table="Permissions" lazy="false">
    
<id name="PermissionID" column="PermissionID" unsaved-value="0">
      
<generator class="native" />
    
</id>
    
<property name="PermissionName" column="PermissionName" type="string" length="64" not-null="true" unique="true"></property>
    
<bag name="RoleList" table="Role_Permissions"  lazy="true">
      
<key column="PermissionID"/>
      
<many-to-many class="NhibernateSample1.Role,NhibernateSample1" column="RoleID"></many-to-many>
    
</bag>
  
</class>
</hibernate-mapping>
4。数据操作类
UserRolePermissionFixure
5。单元测试类
UnitTest1.cs
   通过本篇的学习,将充分理解到nhibernate对级联支持的强大。另外除了支持三级联之外,他还支持异类关联(Heterogeneous Associations) .给开发带来了更多的灵活性和实用性。而且考虑到性能的问题,还添加了lazy这样的延迟加载的功能,加载父亲不必要一定要加载他的儿子集合。通过集合类映射,nhinernate轻松实现级联,这相比较代码生成来说,无疑是一个优点。