当前位置 : 主页 > 编程语言 > 其它开发 >

NHibernate实例分享:Northwind Mapping

来源:互联网 收集:自由互联 发布时间:2022-05-25
NHibernate实例分享:Northwind Mapping 本节内容 说明 代码 参考资料 说明 NHibernate的强大之处在于其映射,我们在设计Domain中,经常为各种各样的映射感到头痛,为此我把微软SQL Server2000自带
NHibernate实例分享:Northwind Mapping

本节内容

  • 说明
  • 代码
  • 参考资料
说明

NHibernate的强大之处在于其映射,我们在设计Domain中,经常为各种各样的映射感到头痛,为此我把微软SQL Server2000自带的Northwind数据库使用NHibernate映射了一下,需要的请下载其代码,在文章中就不仔细说明了,一切都在代码中。

顺便说一句,其实映射不必这么复杂的写出全部:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="YourApp.Common.DataObjects.Address, YourApp"
          table="Address">
       <id name="Id" type="Int32" unsaved-value="null">
           <column name="Id"
                   length="4" sql-type="int"
                   not-null="true" unique="true" index="PK_Address"/>
           <generator class="native" />
       </id>
       <property name="Address1" type="String">
           <column name="Address1" length="255"
                   sql-type="varchar" not-null="true"/>
       </property>
       <property name="Address2" type="String">
           <column name="Address2" length="255"
                   sql-type="varchar" not-null="false"/>
       </property>
       <property name="City" type="String">
           <column name="City" length="255"
                   sql-type="varchar" not-null="true"/>
       </property>
       <property name="State" type="String">
           <column name="`State`" length="2"
                   sql-type="varchar" not-null="true"/>
       </property>
       <property name="Zip" type="String">
           <column name="Zip" length="12"
                   sql-type="varchar" not-null="true"/>
       </property>
       <many-to-one name="Contact" class="YourApp.Common.DataObjects.Contact, YourApp">
           <column name="Contact_Id" length="4"
                   sql-type="int" not-null="false"/>
       </many-to-one>
       <bag name="BillToOrderHeaders" inverse="true" lazy="true" cascade="all-delete-orphan">
           <key column="BillToAddress_Id"/>
           <one-to-many
               class="YourApp.Common.DataObjects.OrderHeader, YourApp"/>
       </bag>
       <bag name="ShipToOrderHeaders" inverse="true" lazy="true" cascade="all-delete-orphan">
           <key column="ShipToAddress_Id"/>
           <one-to-many
               class="YourApp.Common.DataObjects.OrderHeader, YourApp"/>
       </bag>
   </class>
</hibernate-mapping>

其实这样就可以了,不要把默认的都写上去的,我这个例子映射写的最简洁,大家下载看看吧

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="YourApp"
                  namespace="YourApp.Common.DataObjects">
  
   <class name="Address">
       <id name="Id">
           <generator class="native" />
       </id>
       <property name="Address1" not-null="true" />
       <property name="Address2"/>
       <property name="City" not-null="true"/>
       <property name="State" length="2" not-null="true"/>
       <property name="Zip" length="12" not-null="true"/>
       <many-to-one name="Contact" column="Contact_Id"/>
       <bag name="BillToOrderHeaders" inverse="true" cascade="all-delete-orphan">
           <key column="BillToAddress_Id"/>
           <one-to-many class="OrderHeader"/>
       </bag>
       <bag name="ShipToOrderHeaders" inverse="true" cascade="all-delete-orphan">
           <key column="ShipToAddress_Id"/>
           <one-to-many class="OrderHeader"/>
       </bag>
   </class>
</hibernate-mapping>
代码

下载地址:http://code.google.com/p/yjinglee/downloads/list

参考资料

Microsoft: Northwind and pubs Sample Databases for SQL Server 2000

网友评论