首页 > 代码库 > 01-03-02-1【Nhibernate (版本3.3.1.4000) 出入江湖】CRUP操作--cascade 级联相关

01-03-02-1【Nhibernate (版本3.3.1.4000) 出入江湖】CRUP操作--cascade 级联相关

要点:

1.

<!--双向关联时要用:
inverse:由子表来维护关系,cascade:级联的关系 如果没有这个设置,
插入Customer成功(即使现在Order插入Order抛异常,这时产生了垃圾数据,
好的做法是用事务两者都能插入,避免垃圾数据的产生-->
<!--经测试:只需要在主表配置cascade="all",而子表不用配置cascade="all",
当删除主表实体时,子表的级联实体也会被删除-->
<!--cascade有4种??是吗??,分别表示级联操作的4种操作:
cascade = "none": 不能执行操作(有子时不给删父),经测试,为none时,添加父时没有自动添加子
cascade = "all" : 1.级联(删父时删子),经测试,为all时,添加父时自动添加子,
2.经测试,当设置cascade = "all",MS SQL Server设置删除和更新操作虽然设置为:不执行操作,却被覆盖为联级
cascade = "null": (删父时子设置为null),但经测试Nhibernate不支持这个值
cascade = "???": 删父时子设置为默认值-->

<set name="Orders" table="Order" generic="true"
inverse="true" cascade="all">

<key column="CustomerId" foreign-key="FK_CustomerOrders"/>
<one-to-many class="Model.Order,Model"/>
</set>

 



2

 

 

Custmer.cs

 1 using Iesi.Collections.Generic;
 2 using System;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Model
 7 {
 8     public class Customer
 9     {
10         public virtual int CustomerId{ get; set; }
11         public virtual String FirstName { get; set; }
12         public virtual String LastName { get; set; }
13 
14         public virtual int Age { get; set; }
15         public virtual int Version { get; set; }
16 
17         private ISet<Order> _orders = new HashedSet<Order>();
18         public virtual ISet<Order> Orders
19         {
20             get { return _orders; }
21             set { this._orders = value; }
22         }
23 
24     }
25 }

 

Customer.hbm.xml

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
 3                    namespace="Model" 
 4                    assembly="Model" 
 5                    default-lazy="true">
 6   
 7   <class name="Model.Customer, Model"
 8          discriminator-value=http://www.mamicode.com/"0">
 9     <!--unsaved-value=http://www.mamicode.com/"0" 主键表中不需要定义,而是需要在子表中定义-->
10     <id name="CustomerId"
11         column="CustomerId"
12         type="Int32" 
13         unsaved-value=http://www.mamicode.com/"0">
14       <generator class="native" />
15       <!-- unsaved-value used to be null and generator was increment in h2.0.3 -->
16     </id>
17 
18     <!--version标签必须放在Id后面,否则出错-->
19     <version name="Version"/>
20 
21     <property name="FirstName" >
22       <column name="Firstname" length="50" not-null="true"/>
23     </property>
24 
25     <property name="LastName" type="String">
26       <column name="Lastname"/>
27     </property>
28 
29     <property name="Age" type="int">
30       <column name="Age"/>
31     </property>
32 
33     <!--双向关联时要用:
34          inverse:由子表来维护关系,cascade:级联的关系 如果没有这个设置,
35         插入Customer成功(即使现在Order插入Order抛异常,这时产生了垃圾数据,
36         好的做法是用事务两者都能插入,避免垃圾数据的产生-->
37     <!--经测试:只需要在主表配置cascade="all",而子表不用配置cascade="all"38     当删除主表实体时,子表的级联实体也会被删除-->
39     <!--cascade有4种??是吗??,分别表示级联操作的4种操作:
40       cascade = "none": 不能执行操作(有子时不给删父),经测试,为none时,添加父时没有自动添加子
41       cascade = "all" : 1.级联(删父时删子),经测试,为all时,添加父时自动添加子,
42                         2.经测试,当设置cascade = "all",MS SQL Server设置删除和更新操作虽然设置为:不执行操作,却被覆盖为联级
43       cascade = "null": (删父时子设置为null),但经测试Nhibernate不支持这个值
44       cascade = "???": 删父时子设置为默认值-->
45     <set name="Orders" table="Order" generic="true"
46           inverse="true" cascade="all">
47 
48       <key column="CustomerId" foreign-key="FK_CustomerOrders"/>
49       <one-to-many class="Model.Order,Model"/>
50     </set>
51   </class>
52 </hibernate-mapping>

 

 

Order.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class Order
    {
        public virtual int OrderId { get; set; }
        public virtual DateTime OrderDate { get; set; }
        public virtual Customer Customer { get; set; }

        public virtual int Version { get; set; }
    }
}

 

Order.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   namespace="Model" 
                   assembly="Model" 
                   default-lazy="true">
  
  <!--table ="[Order]"因为Order是SQL关键字,表名如果不添加中括号[]会抛异常-->
  <!--unsaved-value=http://www.mamicode.com/"0" 主键表中不需要定义,而是需要在子表中定义
      unsaved-value id的类型是对象类型(如Int32等)unsaved-value不用显示标出来,因为unsaved-value默认值是null
  unsaved-value默认值是null,
  作用是:用于判断实体对象是新new的内存对象,还是应经持久化的对象,Nhibernate会从数据库中去获取
  该实体对象的Id与unsaved-value的值比较(如果持久化,id不等于null,当然也就不等于默认值为unsaved-value):
  
  1.实体对象的Id与unsaved-value的值,相等,说明调用Save()添加,
  2.实体对象的Id与unsaved-value的值,不相等相等,调用Update(),
          
     并在如果
  -->
  <class name="Model.Order, Model" 
         table ="[Order]"
         discriminator-value=http://www.mamicode.com/"0" lazy="false">
    <id name="OrderId" 
        column="OrderId" 
        type="Int32" 
        unsaved-value=http://www.mamicode.com/"0">
      <generator class="native" />
      <!-- unsaved-value used to be null and generator was increment in h2.0.3 -->
    </id>
    
    <!--version标签必须放在Id后面,否则出错-->
    <!--<version name="Version"/>-->
   
    <property name="OrderDate"  type="DateTime"  not-null="true">
      <column name="OrderDate" />
    </property>

    <many-to-one name="Customer" not-null="true">
      <column name="CustomerId" />
    </many-to-one>
    
  </class>
</hibernate-mapping>