NHibernate开源框架Cuyahoga学习之权限映射

代码
在Cuyahoga项目中,NHibernate中关联关系在权限中得到了很好的体现。
User用户
Role角色:比如管理员,会员,游客等
Right权限:比如文章的增,删,改
Site站点 
项目中的三者之间的关系是:
User:Role 关系:多对多单项关联. 多个用户对应一个角色。
Role:Right关系: 多对多单项关联. 多个角色对应一个权限。
Role:Site关系:多对多双向关联
   多对多时,采用关联表的策略,此时两个配置文件中
key和many
- to - many都必须指定column属性,key和many - to - many分别指定本持久化类,
关联类在连接表中的外键列名,因此两边的key与many
- to - many的column属性交叉相同。
-------------------------------------
下面是实体类和配置文件。
1 .实体类User以及hbm.xml配置文件代码:
User实体类
        
public   class  User 
    {
        
private   int  _id;
        
private   string  _userName;
        
private   string  _password;
        
private  IList < Role >  _roles;
        
private  IList < Right >  _rights;
        
#region  properties
        
        
public   virtual   int  Id
        {
            
get  {  return   this ._id; }
            
set  {  this ._id  =  value; }
        }
        [ValidateNonEmpty(
" UserNameValidatorNonEmpty " )]
        [ValidateLength(
1 50 " UserNameValidatorLength " )]
                
public   virtual   string  UserName
                {
                    
get  {  return   this ._userName; }
                    
set  {  this ._userName  =  value; }
                }
        [ValidateNonEmpty(
" PasswordValidatorNonEmpty " )]
        [ValidateLength(
5 50 " PasswordValidatorLength " )]
        
public   virtual   string  Password
        {
            
get  {  return   this ._password; }
            
set  {  this ._password  =  value; }
        }
        [ValidateCollectionNotEmpty(
" RolesValidatorNotEmpty " )]
        
public   virtual  IList < Role >  Roles
        {
            
get  {  return   this ._roles; }
            
set  {  this ._roles  =  value; }
        }
        
public   virtual  IList < Right >  Rights
        {
            
get
            {
                
if  ( this ._rights  ==   null )
                {
                    
this ._rights  =   new  List < Right > ();
                    
foreach  (Role role  in   this ._roles)
                    {
                        
foreach  (Right right  in  role.Rights)
                        {
                            
if  ( !   this ._rights.Contains(right))
                            {
                                
this ._rights.Add(right);
                            }
                        }
                    }
                }
                
return   this ._rights;
            }
        }

        
public  User()
        {
            
this ._id  =   - 1 ;
            
this ._roles  =   new  List < Role > ();
            
        }
            }
User.hbm.xml配置文件
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>  
< hibernate - mapping xmlns = " urn:nhibernate-mapping-2.2 " >
    
< class  name = " Cuyahoga.Core.Domain.User, Cuyahoga.Core "  table = " cuyahoga_user " >

        
< cache usage = " read-write "   />
        
        
< id name = " Id "  column = " userid "  type = " Int32 "  unsaved - value = " -1 " >
            
< generator  class = " native " >
                
< param name = " sequence " > cuyahoga_user_userid_seq </ param >
            
</ generator >
        
</ id >
        
< property name = " UserName "  column = " username "  type = " String "  length = " 50 "   />
        
< property name = " Password "  column = " password "  type = " String "  length = " 100 "   />         
                
<! 多对多单项关联: -- key和many - to - many都必须指定column属性,
                  key和many
- to - many分别指定本持久化类,关联类在连接表中的外键列名,
                  因此两边的key与many
- to - many的column属性交叉相同。cuyahoga_userrole为关联表 -->
                
< bag name = " Roles "  table = " cuyahoga_userrole "  cascade = " none "  lazy = " true " >
            
< cache usage = " read-write "   />
            
< key column = " userid "   />
            
< many - to - many  class = " Cuyahoga.Core.Domain.Role, Cuyahoga.Core "  column = " roleid "  outer -                         join = " true "   />
            
</ bag >
    
</ class >
</ hibernate - mapping >

2 .实体类Role以及hbm.xml配置
   
// 实体类Role
     public   class  Role
    {
        
private   int  _id;
        
private   string  _name;
        
private  IList < Right >  _rights;
        
private  IList < Site >  _sites;
                
public  Role()
        {
            
this ._id  =   - 1 ;
            
this ._name  =   null ;
            
this ._rights  =   new  List < Right > ();
            
this ._sites  =   new  List < Site > ();
        }
        
public   virtual   int  Id
        {
            
get  {  return   this ._id; }
            
set  {  this ._id  =  value; }
        }
        [ValidateNonEmpty(
" RoleNameValidatorNonEmpty " )]
        [ValidateLength(
1 50 " RoleNameValidatorLength " )]
        
public   virtual   string  Name
        {
            
get  {  return   this ._name; }
            
set  {  this ._name  =  value; }
        }
                
public   virtual  IList < Right >  Rights
        {
            
get  {  return  _rights; }
            
set  { _rights  =  value; }
        }

        
public   virtual  IList < Site >  Sites
        {
            
get  {  return  _sites; }
            
set  { _sites  =  value; }
        }
       }
   
// Role.hbm.xml配置文件如下:
   -----------------------------------------------------------------------------
   
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>  
   
< hibernate - mapping xmlns = " urn:nhibernate-mapping-2.2 " >
    
< class  name = " Cuyahoga.Core.Domain.Role, Cuyahoga.Core "  table = " cuyahoga_role " >
        
< cache usage = " read-write "   />
        
< id name = " Id "  column = " roleid "  type = " Int32 "  unsaved - value = " -1 " >
            
< generator  class = " native " >
                
< param name = " sequence " > cuyahoga_role_roleid_seq </ param >
            
</ generator >
        
</ id >
        
< property name = " Name "  column = " name "  type = " String "  length = " 50 "   />
                
<! Role:Right多对多单项关联: ---- 角色:权限 ----->
        
< bag name = " Rights "  cascade = " none "  lazy = " true "  table = " cuyahoga_roleright " >
            
< cache usage = " read-write "   /><!-- 方法到进程间缓存 -->
            
< key column = " roleid "   />
            
< many - to - many  class = " Cuyahoga.Core.Domain.Right, Cuyahoga.Core "   column = " rightid "    />
        
</ bag >
                
<! Role:Site多对多单项关联: ---- 角色:站点   ----->                                                                                                 
        
< bag name = " Sites "  cascade = " none "  lazy = " true "  table = " cuyahoga_siterole " >
            
< key column = " roleid "   />
            
< many - to - many  class = " Cuyahoga.Core.Domain.Site, Cuyahoga.Core "  column = " siteid "   />
        
</ bag >
        
    
</ class >
</ hibernate - mapping >
--------------------------------------------------------------------------------------------------------
 
3 .Right实体类和hbm.xml配置文件
  Right实体类如下: 
  
public   class  Right
    {
        
private   int  _id;
        
private   string  _name;
        
private   string  _description;

        
///   <summary>
        
///  ID
        
///   </summary>
         public   virtual   int  Id
        {
            
get  {  return  _id; }
            
set  { _id  =  value; }
        }

        
///   <summary>
        
///  Name of the right.
        
///   </summary>
         public   virtual   string  Name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }

        
///   <summary>
        
///  Description of the right.
        
///   </summary>
         public   virtual   string  Description
        {
            
get  {  return  _description; }
            
set  { _description  =  value; }
        }

        
///   <summary>
        
///  Constructor.
        
///   </summary>
         public  Right()
        {
            
this ._id  =   - 1 ;
        }
    }
  Right.hbm.xml配置文件    
---------------------------------------------------------------------------------
  
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
  
< hibernate - mapping xmlns = " urn:nhibernate-mapping-2.2 " >
    
< class  name = " Cuyahoga.Core.Domain.Right, Cuyahoga.Core "  table = " cuyahoga_right " >

        
< cache usage = " read-only "   />

        
< id name = " Id "  column = " rightid "  type = " Int32 "  unsaved - value = " -1 " >
            
< generator  class = " native " >
                
< param name = " sequence " > cuyahoga_right_rightid_seq </ param >
            
</ generator >
        
</ id >

        
< property name = " Name "  column = " name "  type = " String "  length = " 50 "   />
        
< property name = " Description "  column = " description "  type = " String "  length = " 255 "   />

    
</ class >
</ hibernate - mapping >     
------------------------------------------------------------------------------------------
4 .Site实体类以及配置文件

Site实体类
public   class  Site
{
        
private   int  _id;
        
private  Template _defaultTemplate;
        
private  Role _defaultRole;
        
private  IList < Node >  _rootNodes;
        
private  IList < Role >  _roles;
        
private  IList < Template >  _templates;
        
private  IList < Category >  _rootCategories;
            
#region  properties
        
public   virtual   int  Id
        {
            
get  {  return   this ._id; }
            
set  {  this ._id  =  value; }
        }
        
public   virtual  Role DefaultRole
        {
            
get  {  return   this ._defaultRole; }
            
set  {  this ._defaultRole  =  value; }
        }
        
public   virtual  IList < Node >  RootNodes
        {
            
get  {  return   this ._rootNodes; }
            
set  {  this ._rootNodes  =  value; }
        }
        
public   virtual  IList < Role >  Roles
        {
            
get  {  return  _roles; }
            
set  { _roles  =  value; }
        }

        
public   virtual  IList < Template >  Templates
        {
            
get  {  return  _templates; }
            
set  { _templates  =  value; }
        }
        
public   virtual  IList < Category >  RootCategories
        {
            
get  {  return  _rootCategories; }
            
set  { _rootCategories  =  value; }
        }
        
public  Site()
        {
            
this ._id  =   - 1 ;
            
this ._rootNodes  =   new  List < Node > ();
            
this ._roles  =   new  List < Role > ();
            
this ._templates  =   new  List < Template > ();
            
this ._rootCategories  =   new  List < Category > ();
        }
    }
Site.hbm.xml配置文件如下:
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< hibernate - mapping xmlns = " urn:nhibernate-mapping-2.2 " >
    
< class  name = " Cuyahoga.Core.Domain.Site, Cuyahoga.Core "  table = " cuyahoga_site " >

        
< cache usage = " read-write "   />

        
< id name = " Id "  column = " siteid "  type = " Int32 "  unsaved - value = " -1 " >
            
< generator  class = " native " >
                
< param name = " sequence " > cuyahoga_site_siteid_seq </ param >
            
</ generator >
        
</ id >
        
< many - to - one name = " DefaultTemplate "   class = " Cuyahoga.Core.Domain.Template, Cuyahoga.Core "  column = " templateid "  not - null = " false "  outer - join = " false "   />
        
< many - to - one name = " DefaultRole "   class = " Cuyahoga.Core.Domain.Role, Cuyahoga.Core "  column = " roleid "  not - null = " true "  outer - join = " true "   />
        
< bag name = " RootNodes "  cascade = " none "   where = " parentnodeid is null "  order - by = " position asc "  lazy = " true " >
            
< key column = " siteid "   />
            
< one - to - many  class = " Cuyahoga.Core.Domain.Node, Cuyahoga.Core "   />
        
</ bag >
        
< bag name = " Roles "  cascade = " none "  table = " cuyahoga_siterole "  lazy = " true " >
            
< key column = " siteid "   />
            
< many - to - many  class = " Cuyahoga.Core.Domain.Role, Cuyahoga.Core "  column = " roleid "   />
        
</ bag >
        
< bag name = " Templates "  cascade = " none "  lazy = " true "  inverse = " true " >
            
< key column = " siteid "   />
            
< one - to - many  class = " Cuyahoga.Core.Domain.Template, Cuyahoga.Core "   />
        
</ bag >
        
< bag name = " RootCategories "  cascade = " none "   where = " parentcategoryid is null "  order - by = " path asc "  lazy = " true "  inverse = " true " >
            
< key column = " siteid "   />
            
< one - to - many  class = " Cuyahoga.Core.Domain.Category, Cuyahoga.Core "   />
        
</ bag >
    
</ class >
</ hibernate - mapping >

 

你可能感兴趣的:(Hibernate)