频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

NHibernate:One-to-Many一对多映射(VB.NET版)

来源: 作者: 出处:巧巧读书 2007-03-15 进入讨论组
     /* 作者:飞鹰 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
   上面程序在我的机器上运行通过。
  URl收藏 http://www.qqread.com/aspdotnet/a303239.html 更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章