Nhhibernate中many-to-one和one-to-many用法

Nhhibernate中many-to-one和one-to-many用法

 

表相应的类和映射文件为如下

Categorier

using System;

using Iesi.Collections;

namespace Product

{

     /// <summary>

     /// Categories 的摘要说明。

     /// </summary>

     public class Categories

     {

         public Categories()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

         }

         private int categoryID;

         private string categoryName;

         private string description;

         private byte[] picture;

         private Set productList = new HashedSet();

 

         public int CategoryID

         {

              get { return categoryID; }

              set { categoryID = value; }

         }

 

         public string CategoryName

         {

              get { return categoryName; }

              set { categoryName = value; }

         }

 

         public string Description

         {

              get { return description; }

              set { description = value; }

         }

 

         public byte[] Picture

         {

              get { return picture; }

              set { picture = value; }

         }

 

         public Set ProductList

         {

              get { return productList; }

              set { productList = value; }

         }

     }

}

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

     <class name="Product.Categories,Product" table="Categories">

         <id name="CategoryID" column="CategoryID" type="Int32" length="4">

              <generator class="identity"/>

         </id>

         <property name="CategoryName" type="string" length="15"/>

         <property name="Description" type="string"/>

         <property name="Picture" type="Byte[]"/>

         <!--<bag name="ProductList" cascade="all" inverse="true">

              <key column="CategoryId"/>

              <one-to-many class="Product.Products,Product"/>

         </bag>-->

         <set name="ProductList" cascade="all" inverse="true" lazy="false">

              <key>

                   <column name="CategoryID"/>

              </key>

              <one-to-many class="Product.Products,Product"/>

         </set>

     </class>

</hibernate-mapping>

 

Products

using System;

using NHibernate.Collection;

using Iesi.Collections;

namespace Product

{

     /// <summary>

     /// Products 的摘要说明。

     /// </summary>

     public class Products

     {

         public Products()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

         }

         private int productID;

         private string productName;

         private int supplierID;

         private int categoryID;

         private string quantityPerUnit;

         private double unitPrice;

         private short unitsInStock;

         private short unitsOnOrder;

         private short reorderLevel;

         private byte discontinued;

 

         private Categories cate;//  = new Categories();

 

         public int ProductID

         {

              get { return productID; }

              set { productID = value; }

         }

 

         public string ProductName

         {

              get { return productName; }

              set { productName = value; }

         }

 

         public int SupplierID

         {

              get { return supplierID; }

              set { supplierID = value; }

         }

 

         public int CategoryID

         {

              get { return categoryID; }

              set { categoryID = value; }

         }

 

         public string QuantityPerUnit

         {

              get { return quantityPerUnit; }

              set { quantityPerUnit = value; }

         }

 

         public double UnitPrice

         {

              get { return unitPrice; }

              set { unitPrice = value; }

         }

 

         public short UnitsInStock

         {

              get { return unitsInStock; }

              set { unitsInStock = value; }

         }

 

         public short UnitsOnOrder

         {

              get { return unitsOnOrder; }

              set { unitsOnOrder = value; }

         }

 

         public short ReorderLevel

         {

              get { return reorderLevel; }

              set { reorderLevel = value; }

         }

 

         public byte Discontinued

         {

              get { return discontinued; }

              set { discontinued = value; }

         }

 

         public Categories Cate

         {

              get { return cate; }

              set { cate = value; }

         }

     }

}

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

     <class name="Product.Products,Product" table="Products">

         <id name="ProductID" column="ProductID" type="Int32" length="4">

              <generator class="native"/>

         </id>

         <property name="ProductName" type="string" length="40"/>

         <property name="SupplierID" type="Int32" length="4"/>

         <property name="CategoryID" type="Int32" length="4"/>

         <property name="QuantityPerUnit" type="string" length="20"/>

         <property name="UnitPrice" type="Double" length="8"/>

         <property name="UnitsInStock" type="Int16" length="2"/>

         <property name="UnitsOnOrder" type="Int16" length="2"/>

         <property name="ReorderLevel" type="Int16" length="2"/>

         <property name="Discontinued" type="Byte" length="1"/>

         <many-to-one name="Cate" column="CategoryID" class="Product.Categories,Product" unique="true" insert="false" update="false"/>

     </class>

</hibernate-mapping>

 

配置文件:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">

     <session-factory name="NHibernateAndDatagrid">

         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>

         <property name="connection.dirver_class">NHibernate.Driver.SqlClientDriver</property>

         <property name="connection.connection_string">Server=192.168.1.74;initial catalog=Test;user id=sa;password=</property>

         <property name="show_sql">false</property>

         <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>

         <property name="user_outer_join">true</property>

         <property name="command_timeout">20</property>

         <property name="query.substitutions">true 1,false 0,yes 1,no 0</property>

     </session-factory>

</hibernate-configuration>

posted on 2008-12-17 20:22  张皓  阅读(850)  评论(0编辑  收藏  举报

导航