帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP.NET编程
NHibernate: One-to-Many一对多映射(VB.NET版)
作者:飞鹰 发布时间:2005-03-12 来源:ASPCOOL
     /*
   作者:飞鹰
  
   ASP酷技术资讯网(www.ASPCool.com)版权所有,如转载,请保留此信息.
   */
  
  终于使用NHibernate作为项目研究的ORM框架,这次研究只是为了证明一件事,那就是使用ORM后可以提高程序的开发效率和优化程序的结构。
  
  由于CRUD都可以实现了,所以,我就参照张老三的文章来做One-To-Many的例子。这里我使用SQL Server自带的NorthWind中的两个表Customers,Orders来做例子,我把Customers类作为父类,Orders类作为子类。
  
  我们先用[url=http://blog.aspcool.com/tim/posts/176.aspx]Cool Coder[/url]生成XML文件和C#代码,再用[url=http://authors.aspalliance.com/aldotnet/examples/translate.aspx]C#2VB转换器[/url]把C#代码转变成VB代码(为什么要转来转去呢?等过几天有空了,我升级Cool Coder,使之也可以生成VB代码,先临时凑合着用吧!)。稍作修改后就可以得到下面的内容。
  
  先看Customers的映射信息:
  
  xml version="1.0" encoding="utf-8" ?>
  
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  
   <class name="Customers, AssemblyName" table="Customers">
  
   <id name="CustomerID" column="CustomerID" type="String(5)">
  
   <generator class="assigned" />
  
   id>
  
   <set name="Orders" inverse="true" lazy="true">
  
   <key column="CustomerID" />
  
   <one-to-many class="Orders, AssemblyName " />
  
   set>
  
   <property name="CompanyName" type="String(40)" column="CompanyName" />
  
   <property name="ContactName" type="String(30)" column="ContactName" />
  
   <property name="ContactTitle" type="String(30)" column="ContactTitle" />
  
   <property name="Address" type="String(60)" column="Address" />
  
   <property name="City" type="String(15)" column="City" />
  
   <property name="Region" type="String(15)" column="Region" />
  
   <property name="PostalCode" type="String(10)" column="PostalCode" />
  
   <property name="Country" type="String(15)" column="Country" />
  
   <property name="Phone" type="String(24)" column="Phone" />
  
   <property name="Fax" type="String(24)" column="Fax" />
  
   class>
  
  hibernate-mapping>
  
  
  
   Customers类的代码如下:
  
  Imports System
  
  Public Class Customers
  
   Public Sub New()
  
   End Sub 'New
  
   Dim m_orderList As IDictionary = New Hashtable
  
  
  
   '*
  
   '* Property OrderList ( IDictionary)
  
   '*
  
   Public Property Orders() As IDictionary
  
   Get
  
   Return m_orderList
  
   End Get
  
   Set(ByVal Value As IDictionary)
  
   m_orderList = Value
  
   End Set
  
   End Property
  
  
  
   Private _Region As System.String
  
  
  
   Public Property [Region]() As System.String
  
   Get
  
   Return _Region
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _Region = Value
  
   End Set
  
   End Property
  
   Private _PostalCode As System.String
  
  
  
   Public Property PostalCode() As System.String
  
   Get
  
   Return _PostalCode
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _PostalCode = Value
  
   End Set
  
   End Property
  
   Private _Fax As System.String
  
  
  
   Public Property Fax() As System.String
  
   Get
  
   Return _Fax
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _Fax = Value
  
   End Set
  
   End Property
  
   Private _ContactName As System.String
  
  
  
   Public Property ContactName() As System.String
  
   Get
  
   Return _ContactName
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ContactName = Value
  
   End Set
  
   End Property
  
   Private _City As System.String
  
  
  
   Public Property City() As System.String
  
   Get
  
   Return _City
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _City = Value
  
   End Set
  
   End Property
  
   Private _CustomerID As System.String
  
  
  
   Public Property CustomerID() As System.String
  
   Get
  
   Return _CustomerID
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _CustomerID = Value
  
   End Set
  
   End Property
  
   Private _Address As System.String
  
  
  
   Public Property Address() As System.String
  
   Get
  
   Return _Address
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _Address = Value
  
   End Set
  
   End Property
  
   Private _ContactTitle As System.String
  
  
  
   Public Property ContactTitle() As System.String
  
   Get
  
   Return _ContactTitle
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ContactTitle = Value
  
   End Set
  
   End Property
  
   Private _Phone As System.String
  
  
  
   Public Property Phone() As System.String
  
   Get
  
   Return _Phone
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _Phone = Value
  
   End Set
  
   End Property
  
   Private _CompanyName As System.String
  
  
  
   Public Property CompanyName() As System.String
  
   Get
  
   Return _CompanyName
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _CompanyName = Value
  
   End Set
  
   End Property
  
   Private _Country As System.String
  
  
  
   Public Property Country() As System.String
  
   Get
  
   Return _Country
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _Country = Value
  
   End Set
  
   End Property
  
  End Class 'Customers
  
  
  
  
  
  
  
   Orders类的映射信息如下:
  
  xml version="1.0" encoding="utf-8" ?>
  
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  
   <class name="Orders, AssemblyName " table="Orders">
  
   <id name="OrderID" column="OrderID" type="Int32">
  
   <generator class="identity" />
  
   id>
  
  
  
   <many-to-one name="Customers" column="CustomerID" class="Customers, AssemblyName "/>
  
   <property name="EmployeeID" type="Int32" column="EmployeeID" />
  
   <property name="OrderDate" type="DateTime" column="OrderDate" />
  
   <property name="RequiredDate" type="DateTime" column="RequiredDate" />
  
   <property name="ShippedDate" type="DateTime" column="ShippedDate" />
  
   <property name="ShipVia" type="Int32" column="ShipVia" />
  
   <property name="Freight" type="Decimal" column="Freight" />
  
   <property name="ShipName" type="String(40)" column="ShipName" />
  
   <property name="ShipAddress" type="String(60)" column="ShipAddress" />
  
   <property name="ShipCity" type="String(15)" column="ShipCity" />
  
   <property name="ShipRegion" type="String(15)" column="ShipRegion" />
  
   <property name="ShipPostalCode" type="String(10)" column="ShipPostalCode" />
  
   <property name="ShipCountry" type="String(15)" column="ShipCountry" />
  
   class>
  
  hibernate-mapping>
  
  
  
   Orders类的代码如下:
  
  _
  
  Public Class Orders
  
  
  
   Public Sub New()
  
   End Sub 'New
  
  
  
   Private _OrderDate As System.DateTime
  
  
  
   Public Property OrderDate() As System.DateTime
  
   Get
  
   Return _OrderDate
  
   End Get
  
   Set(ByVal Value As System.DateTime)
  
   _OrderDate = value
  
   End Set
  
   End Property
  
   Private _ShipName As System.String
  
  
  
   Public Property ShipName() As System.String
  
   Get
  
   Return _ShipName
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipName = value
  
   End Set
  
   End Property
  
   Private _ShippedDate As System.DateTime
  
  
  
   Public Property ShippedDate() As System.DateTime
  
   Get
  
   Return _ShippedDate
  
   End Get
  
   Set(ByVal Value As System.DateTime)
  
   _ShippedDate = value
  
   End Set
  
   End Property
  
   Private _ShipPostalCode As System.String
  
  
  
   Public Property ShipPostalCode() As System.String
  
   Get
  
   Return _ShipPostalCode
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipPostalCode = value
  
   End Set
  
   End Property
  
   Private _ShipCity As System.String
  
  
  
   Public Property ShipCity() As System.String
  
   Get
  
   Return _ShipCity
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipCity = value
  
   End Set
  
   End Property
  
   Private _ShipAddress As System.String
  
  
  
   Public Property ShipAddress() As System.String
  
   Get
  
   Return _ShipAddress
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipAddress = value
  
   End Set
  
   End Property
  
   Private _ShipCountry As System.String
  
  
  
   Public Property ShipCountry() As System.String
  
   Get
  
   Return _ShipCountry
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipCountry = value
  
   End Set
  
   End Property
  
   Private _OrderID As System.Int32
  
  
  
   Public Property OrderID() As System.Int32
  
   Get
  
   Return _OrderID
  
   End Get
  
   Set(ByVal Value As System.Int32)
  
   _OrderID = value
  
   End Set
  
   End Property
  
   Private _RequiredDate As System.DateTime
  
  
  
   Public Property RequiredDate() As System.DateTime
  
   Get
  
   Return _RequiredDate
  
   End Get
  
   Set(ByVal Value As System.DateTime)
  
   _RequiredDate = value
  
   End Set
  
   End Property
  
   Private _EmployeeID As System.Int32
  
  
  
   Public Property EmployeeID() As System.Int32
  
   Get
  
   Return _EmployeeID
  
   End Get
  
   Set(ByVal Value As System.Int32)
  
   _EmployeeID = value
  
   End Set
  
   End Property
  
   Private _ShipVia As System.Int32
  
  
  
   Public Property ShipVia() As System.Int32
  
   Get
  
   Return _ShipVia
  
   End Get
  
   Set(ByVal Value As System.Int32)
  
   _ShipVia = value
  
   End Set
  
   End Property
  
   Private _Freight As System.Decimal
  
  
  
   Public Property Freight() As System.Decimal
  
   Get
  
   Return _Freight
  
   End Get
  
   Set(ByVal Value As System.Decimal)
  
   _Freight = value
  
   End Set
  
   End Property
  
   Private _ShipRegion As System.String
  
  
  
   Public Property ShipRegion() As System.String
  
   Get
  
   Return _ShipRegion
  
   End Get
  
   Set(ByVal Value As System.String)
  
   _ShipRegion = value
  
   End Set
  
   End Property
  
  
  
   Public Property Customers() As Customers
  
   Get
  
   Return _Customer
  
   End Get
  
   Set(ByVal Value As Customers)
  
   _Customer = Value
  
   End Set
  
   End Property
  
   Private _Customer As Customers
  
  End Class 'Orders
  
  
  
   下面给出调用代码,如下:
  
   ExportSchema(New String() {"Customers.hbm.xml", "Orders.hbm.xml"}, False)
  
   Dim s As ISession = sessions.OpenSession()
  
   Dim t As ITransaction = s.BeginTransaction()
  
   Try
  
   Dim customer As New Customers
  
   Dim order As New Orders
  
   order.ShipCity = "GuangDong"
  
   order.EmployeeID = 1
  
   order.ShipVia = 2
  
   order.Customers = customer
  
   With customer
  
   .Address = "Softwarepark"
  
   .City = "Xian"
  
   .CompanyName = "GPCT111"
  
   .ContactName = "Tim"
  
   .ContactTitle = "title"
  
   .Country = "China"
  
   .Region = "Asia"
  
   .CustomerID = "023"
  
   .Orders.Add(order, order)
  
   End With
  
   s.Save(customer)
  
   t.Commit()
  
   Catch ex As Exception
  
   t.Rollback()
  
   Throw ex
  
   Finally
  
   s.Close()
  
   End Try
  
  
  
   上面程序在我的机器上运行通过。
  
  

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·ASP.NET中为DataGrid添加合计字段
·.text urlRewrite介绍
·利用 ASP.NET 的内置功能抵御 Web
·ASP.NET Cache
·用 WebClient.UploadData 方法 上载
·ASP.NET 程序设计-序
·什么是客户端/伺服端(Client/Serve
·因特网应用程序的开发
·网页的种类
·.NET Framework-Microsoft Visual
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统