Active Record学习笔记(三):处理One-To-Many映射

本来打算一口气把Castle.ActiveRecord的学习笔记写完,没想到前段时间太忙了,一放就放了半个月,现在继续未完成的学习笔记吧!

这篇学习笔记主要介绍ActiveRecord如何处理one-to-many的映射,对于many-to-one映射只需把one-to-many的处理过程反过来就可以了。本文涉及了两个实体类User、Address,两个类的关系是一对多,下面是这两个类的实体关系图:


主要内容:
1.编写数据库
2.HasMany和BelongsTo属性说明
3.编写实体类
4.编写表示层调用代码

一、编写数据库
None.gif -- User类对应的数据表
None.gif
Create   Table   [ Users ]
None.gif(
None.gif    
[ ID ]   Int   Identity ( 1 , 1 Primary   Key ,
None.gif    
[ LoginName ]   Varchar ( 50 not   null ,
None.gif    
[ Password ]   Varchar ( 20 not   null
None.gif)
None.gif
None.gif
-- Address类对应的数据表
None.gif
Create   Table   [ Address ]
None.gif(
None.gif ID 
int   identity ( 1 , 1 primary   key ,
None.gif Province 
Varchar ( 50 ),
None.gif City 
Varchar ( 50 ),
None.gif uID 
int   FOREIGN   KEY   REFERENCES  Users (ID)
None.gif)

二、HasMany和BelongsTo属性说明
  HasMany属性:此属性用于代替NHibernate配置文件中的<many-to-one>标签;该属性用在“一”的类中(父对象,本文为User类),指出与“多”的类(级联对象,本文为Address类)的对应关系。具有以下几个主要子属性:
  1.Type:指出关联对象的类名,相当于<many-to-one>标签中的class属性;
  2.Table:指出关联对象的类对应的数据表(本文的数据表Address);
  3.ColumnKey:指出关联对象对应的数据表中指向主类数据表的字段名(本文数据表Address中的字段uID);
  4.Cascade:指明哪些操作会从父对象级联到关联的对象,相当于<many-to-one>标签中的cascade 属性。该属性值应为CascadeEnum枚举值之一:
    a).All:表示父对象的任何操作都会关联到级联对象;
    b).Delete:表示只有对父对象进行删除操作时才会关联到级联对象;
    c).SaveUpdate:表示只有对父对象进行保存、更新操作时才会关联到级联对象;
    d).None(默认值):表示对父对象的任何操作都不会关联到级联对象;
  5.Lazy:指出是否延迟加载级联对象,其属性值为true或false,二者选一;
  6.Where:指定一个附加SQL的Where子句,这里应该写HQL语句;
  7.OrderBy:指定排序方式,这里应该写HQL语句;
  8.Inverse:指定父对象是否级联到子对象;

  BelongsTo属性:此属性用于代替NHibernate配置文件中表示<map>、<set>、<list>、<bag>之类的标签;该属性用在父对象中,指出与级联对象的对应关系。具有以下几个主要子属性:
  1.Cascad:指出是否级联操作,其值于HasMany属性中的Cascade属性一样;
  2.Column:指出数据表中指向父对象数据表的字段名;
  3.Insert:是否允许插入;
  4.Update:是否允许更新;
  5.OuterJoin:是否允许外联抓取,相当于<many-to-one>标签中的outerjoin属性,其值应为OuterJoinEnum枚举值之一:
    a).Auto(默认值):使用外连接抓取关联(对象),如果被关联的对象没有代理(proxy) ;
    b).True:一直使用外连接来抓取关联;
    c).False:永远不使用外连接来抓取关联;

三、编写类体类
  1.编写实体类:User:
ExpandedBlockStart.gif ContractedBlock.gif /**/ ///jailusd@hotmail.com
ExpandedBlockEnd.gif
///2006-09-11

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*数据库脚本
InBlock.gifCreate Table [Users]
InBlock.gif(
InBlock.gif    [ID] Int Identity(1,1) Primary Key,
InBlock.gif    [LoginName] Varchar(50) not null,
InBlock.gif    [Password] Varchar(20) not null
InBlock.gif)
ExpandedBlockEnd.gif
*/

None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  Castle.ActiveRecord;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  OneToMany
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord(
"Users")]
InBlock.gif    
public class User : ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intID;
InBlock.gif        
private string strName;
InBlock.gif        
private string strPassword;
InBlock.gif
InBlock.gif        
private IList AddressList = new ArrayList();
InBlock.gif
InBlock.gif        [PrimaryKey(PrimaryKeyType.Identity, 
"ID")]
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn intID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ intID = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Property(
"LoginName")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn strName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ strName = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Property]
InBlock.gif        
public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn strPassword; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ strPassword = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [HasMany(
typeof(Address), Table = "Address", ColumnKey = "uID")]
InBlock.gif        
public IList Address
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return AddressList;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AddressList 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  2.编写实体类:Address
ExpandedBlockStart.gif ContractedBlock.gif /**/ ///jailusd@hotmail.com
ExpandedBlockEnd.gif
///2006-09-11
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*数据库脚本
Create Table [Address]
(
 ID int identity(1,1) primary key,
 Province Varchar(50),
 City Varchar(50),
 uID int FOREIGN KEY REFERENCES Users (ID)
)
ExpandedBlockEnd.gif
*/

None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  Castle.ActiveRecord;
None.gif
None.gif
namespace  OneToMany
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord(
"Address")]
InBlock.gif    
public class Address :ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intID;
InBlock.gif        
private string strProvince;
InBlock.gif        
private string strCity;
InBlock.gif
InBlock.gif        
private User objUser;
InBlock.gif    
InBlock.gif        [PrimaryKey(PrimaryKeyType.Native,
"ID")]
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return intID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intID 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 省
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Property]
InBlock.gif        
public string Province
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strProvince;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strProvince 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 城市
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Property]
InBlock.gif        
public string City
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strCity;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strCity 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [BelongsTo(
"uId")]
InBlock.gif        
public User User
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (objUser == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    objUser 
= new User();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return objUser;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                objUser 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  3.编写配置文件(与以前一样)

四、编写表示层调用代码(只列举新增的代码)
None.gif private   void  btnAddNewUser_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IConfigurationSource source 
= System.Configuration.ConfigurationSettings.GetConfig("activerecord"as IConfigurationSource;
InBlock.gif    ActiveRecordStarter.Initialize(source, 
typeof(User), typeof(Address));
InBlock.gif
InBlock.gif    
//使用事务处理
InBlock.gif
    using (TransactionScope tran = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            User objUser 
= new User();
InBlock.gif            objUser.Name 
= "jailu";
InBlock.gif            objUser.Password 
= "123456789";
InBlock.gif
InBlock.gif            objUser.Save();
InBlock.gif
InBlock.gif            Address objAddress;
InBlock.gif
InBlock.gif            
for (int i = 0; i < 5; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                objAddress 
= new Address();
InBlock.gif                objAddress.Province 
= "Province" + i.ToString();
InBlock.gif                objAddress.City 
= "City" + i.ToString();
InBlock.gif
InBlock.gif                objAddress.User 
= objUser;
InBlock.gif                objAddress.Save();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            tran.VoteCommit();  
//执行事务
InBlock.gif

InBlock.gif            MessageBox.Show(
"Success!");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tran.VoteRollBack();    
//若出现异常,回滚事务
InBlock.gif
            MessageBox.Show("Fail!" + ex.Message);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值