博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nhibernate学习之简单组合的映射
阅读量:7283 次
发布时间:2019-06-30

本文共 2100 字,大约阅读时间需要 7 分钟。

1.学习目标
   通过学习nhibernate基础知识,了解nhibernate对组合属性的用法。
2.开发环境和必要准备
   开发环境为:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
  必要准备:学习前六篇nhibernate学习系列   , , , , ,
3.分析
   在关系数据库中,为了减少冗余,每个字段会被设计的更加独立。比如一个人的名字,包括名(FirstName),姓(LastName),通常在数据库中要设计为两个字段,这样便于检索所有姓张的用户,而在对象中,一个人的姓名往往是一个组合元素,nhibernate对组合属性也有很好的支持
4.实现方法
   用于组织姓名的对象UserName  
None.gif
public
 
class
 UserName
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
InBlock.gif        
private string _firstName;
InBlock.gif        
private string _lastName;
InBlock.gif        
public string FirstName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _firstName;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _firstName
=value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        
public string LastName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _lastName;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _lastName 
= value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedBlockEnd.gif    }
用户对象:CompositeUser.cs
None.gif
 
public
 
class
 CompositeUser
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
InBlock.gif        
int _uid;
InBlock.gif        UserName _name;
InBlock.gif        
public int Uid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _uid;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _uid 
= value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public UserName Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _name;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _name 
= value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
用户对象的映射文件.CompositeUser.hbm.xml
None.gif
<?
xml version="1.0" encoding="utf-8" 
?>
None.gif
<
hibernate-mapping 
xmlns
="urn:nhibernate-mapping-2.2"
>
None.gif    
<
class 
name
="NhibernateSample1.CompositeUser,NhibernateSample1"
 table
="CompostName"
 lazy
="false"
>
None.gif        
<
id 
name
="Uid"
 column
="Uid"
 unsaved-value
="0"
>
None.gif            
<
generator 
class
="native"
 
/>
None.gif        
</
id
>
None.gif        
<
component 
name
="Name"
 class
="NhibernateSample1.UserName,NhibernateSample1"
>
None.gif            
<
property 
name
="FirstName"
 column
="FirstName"
></
property
>
None.gif            
<
property 
name
="LastName"
 column
="LastName"
></
property
>
None.gif        
</
component
>
None.gif    
</
class
>
None.gif
</
hibernate-mapping
>
注意,compenent就是映射组合属性的节点,它的子节点property代表其组合属性
CompositeUser数据表的表结构为
新增一个CompositeUser对象
None.gif
 
public
 
int
 AddCompositeUser(CompositeUser u)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            session 
= NhibernateSample1.NHibernateHelper.GetCurrentSession();
InBlock.gif            ITransaction tra 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int res =(int)session.Save(u);
InBlock.gif                tra.Commit();
InBlock.gif                
return res;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tra.Rollback();
ExpandedSubBlockEnd.gif            }
InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Close();
ExpandedSubBlockEnd.gif            }
InBlock.gif            
return -1;
ExpandedBlockEnd.gif        }
测试代码
None.gif
  [TestMethod]
None.gif        
public
 
void
 Test1()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            usf.Configure();
InBlock.gif            usf.ExportTables();
InBlock.gif            CompositeUser u 
= new CompositeUser();
InBlock.gif            UserName name 
= new UserName();
InBlock.gif            name.FirstName 
= "zhang";
InBlock.gif            name.LastName 
= "jill";
InBlock.gif            u.Name 
= name;
InBlock.gif            
int res =new NhibernateSample1.UserFixure().AddCompositeUser(u);
InBlock.gif            Assert.IsTrue(res
>0);
ExpandedBlockEnd.gif        }
nhibernate真的是非常强大的一个框架,下篇将学习复杂组合的使用方法。今天先到这。睡了
你可能感兴趣的文章
用C#进行CCITT校验函数
查看>>
数据库安装完成后,必改系统参数之一
查看>>
php的介绍
查看>>
Linux下搭建MySQL的主从复制(一)
查看>>
第十节:python异常处理、类
查看>>
hive基本操作
查看>>
20171226-Win10英文版系统显示中文乱码的解决方案
查看>>
RHEL6 64位ASM方式安装oracle 11gR2(一)
查看>>
apt-get和pkgsrc
查看>>
意外断电造成RAID 5阵列卡数据故障的恢复方法
查看>>
使用Xshell密钥认证机制远程登录Linux
查看>>
A new node couldn't be inserted because one with the same name exists. (VERR_CFGM_NODE_EXISTS).
查看>>
您真的会玩KMS吗_03.排错篇
查看>>
bootstrap,jquery利用后台传入的json数据创建动态表格
查看>>
Azure 中国篇之应用服务--Azure目录同步
查看>>
golang在window环境下用cgo编译c语言
查看>>
在 CentOS 5.0 中安装 RPMforge 安装源
查看>>
activiti与spring集成
查看>>
我如果能在内核中很方便地使用HIGHUSER内存该有多好...
查看>>
StarUML 5.0问题解决:Failed to open the model file. Invalid file format.
查看>>