<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="DAL.Parent, DAL" table="Parent">
<id name="Id" type="Int32" unsaved-value="null">
<column name="ParentID" length="4" sql-type="int" not-null="true" unique="true" index="PK_PARENT"/>
<generator class="native" />
</id>
<property name="ParentName" type="String">
<column name="ParentName" length="20" sql-type="varchar" not-null="false"/>
</property>
<bag name="Childs" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="ParentID"/>
<one-to-many class="DAL.Child, DAL"/>
</bag>
</class>
</hibernate-mapping><?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="DAL.Child, DAL" table="Child">
<id name="Id" type="Int32" unsaved-value="null">
<column name="ChildID" length="4" sql-type="int" not-null="true" unique="true" index="PK_CHILD"/>
<generator class="native" />
</id>
<property name="ChildName" type="String">
<column name="ChildName" length="20" sql-type="varchar" not-null="false"/>
</property>
<many-to-one name="Parent" class="DAL.Parent, DAL">
<column name="ParentID" length="4" sql-type="int" not-null="false"/>
</many-to-one>
</class>
</hibernate-mapping>
代码如下
Parent p = new Parent();
            p.ParentName = "Parent";            Child c = new Child();
            c.ChildName = "Child1";
            c.Parent = p;            p.Childs.Add(c);            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            cfg.AddAssembly("DAL");
            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction t = session.BeginTransaction();            session.Save(p);
            t.Commit();
            session.Close();跟踪后,第二个child语句居然为update不知道为什么?