On Jun 28, 9:15 pm, V <
vijay.s.g...@gmail.com> wrote:
> > Yes but, as I said in the second answer, the final behaviour (of what NH
> > will do) depend on the type of the collection (an <set> will have a
> > different behaviour of a <bag>).
>
Hello again friends.
I created a small demo and it works fine except one issue. If I get a
parent with some children from WCF, remove one child, add a new one,
update an existing one, and finally save the parent again, the child
that was deleted from client does not get deleted from database. I
dumped SQL into log file and saw there was no delete statement issued.
I am posting my hbm files and the demo code used for saving the
parent.
Parent HBM:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="">
<class name="BackendService.Parent, FrontEnd, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="tbl_parent"
xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Guid" unsaved-
value="00000000-0000-0000-0000-000000000000" column="id">
<generator class="guid" />
</id>
<property name="Description" type="String">
<column name="description" />
</property>
<property name="Age" type="Int32">
<column name="age" />
</property>
<set name="Children" lazy="true" cascade="all-delete-orphan"
fetch="select" inverse="true">
<key column="Parent_id" />
<one-to-many class="BackendService.Child, FrontEnd,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</set>
</class>
</hibernate-mapping>
Child HBM:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="">
<class name="BackendService.Child, FrontEnd, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="tbl_child"
xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Guid" unsaved-
value="00000000-0000-0000-0000-000000000000" column="id">
<generator class="guid" />
</id>
<property name="Description" type="String">
<column name="description" />
</property>
<property name="Age" type="Int32">
<column name="age" />
</property>
<many-to-one cascade="save-update" fetch="join" name="ParentRef"
column="parent_id" />
</class>
</hibernate-mapping>
Code:
public void SaveParent(Parent parent)
{
try
{
var sessionFactory = MappingHelper.CreateSesionFactory
();
using (var session = sessionFactory.OpenSession())
{
session.FlushMode = FlushMode.Never;
session.Replicate(parent,
ReplicationMode.Overwrite);
session.SaveOrUpdate(parent);
// if used alone, SaveOrUpdate causes Expected 1,
Actual 0 exception and it does not save children
session.Flush();
}
}
catch(Exception ex)
{
LogException(ex);
throw;
}
}
PS: MappingHelper.CreateSesionFactory(); creates session factory only
once, saves it in a static variable and returns the same on every
subsequent invocation.
Thanks
V