/*
作者:飞鹰
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/aldo.net/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
上面程序在我的机器上运行通过。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器