|
|
| NHibernate: One-to-Many一对多映射(VB.NET版) |
| 作者:不详 来源:不详 发布时间:2006-8-14 9:57:14 发布人:chinazhan |
减小字体
增大字体
/* 作者:飞鹰 ASP酷技术资讯网(www.ASPCool.com)版权所有,如转载,请保留此信息. */ 终于使用NHibernate作为项目研究的ORM框架,这次研究只是为了证明一件事,那就是使用ORM后可以提高程序的开发效率和优化程序的结构。 由于CRUD都可以实现了,所以,我就参照张老三的文章来做One-To-Many的例子。这里我使用SQL Server自带的NorthWind中的两个表Customers,Orders来做例子,我把Customers类作为父类,Orders类作为子类。 我们先用aspcool.com/tim/posts/176.aspx" TARGET=_blank>Cool Coder生成XML文件和C#代码,再用aspalliance.com/aldotnet/examples/translate.aspx" TARGET=_blank>C#2VB转换器把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 上面程序在我的机器上运行通过。 做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。
|
| |
|
[]
[返回上一页]
[打 印]
[收 藏] |
|
| ∷相关文章评论∷ (评论内容只代表网友观点,与本站立场无关!) [更多评论...] |
|
|