1.2 sql server 2000.
Hi,
I was trying to delete a children element of a parent (one-to-many), and I got "deleted object would be re-saved by cascade", then i noticed in my parent mapping, I had a bag of children element with cascade="all", so I changed to "none" and the error is gone.
but this caused another problem. When I try to save the parent (and attached the children along with it), i get
"object references an unsaved transient instance - save the transient instance before flushing"
hm.. okay. So I changed the casecade="save-update" (and also tried "all-delete-orphan", and now saving parent(with children attached to it), agian it's fine, but back to delete children again, get the original error "deleted object would be re-saved by cascade".
I went back to documentation and now I'm a bit confused.. how do I delete the chidlren? and when should I use cascade="all" v.s "save", "delete" and etc?
From page 79 of the pdf reference guide
"Mapping an association (many-to-one, or collection) with cascade="all" marks the association as a parent/
child style relationship where save/update/deletion of the parent results in save/update/deletion of the
child(ren). Futhermore, a mere reference to a child from a persistent parent will result in save / update of the
child. The metaphor is incomplete, however. A child which becomes unreferenced by its parent is not automatically
deleted, except in the case of a <one-to-many> association mapped with cascade="all-delete-orphan"
here is my parent mapping file
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Jumptree.Forum.BusinessEntities"
namespace="Jumptree.Forum.BusinessEntities"
>
<class name="JumptreeForum_Discussions" lazy="false">
<id name="DiscussionID">
<generator class="native" />
</id>
<property name="DiscussionTitle" />
<property name="DiscussionLastPostedOn" />
<property name="DiscussionDefaultComment" />
<property name="CreatedBy" />
<property name="CreatedByIP" />
<property name="CreatedOn" />
<property name="UpdatedBy" />
<property name="UpdatedOn" />
<bag name="Categories" table="JumptreeForum_DiscussionsCategories" lazy="true">
<key column="DiscussionID" />
<many-to-many class="JumptreeForum_Categories" column="CategoryID" />
</bag>
<bag name="Comments"
order-by="CreatedOn"
lazy="true"
cascade="none" >
<key column="DiscussionID" />
<one-to-many class="JumptreeForum_DiscussionComments" />
</bag>
</class>
</hibernate-mapping>
here is my child mapping
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Jumptree.Forum.BusinessEntities"
namespace="Jumptree.Forum.BusinessEntities"
>
<class name="JumptreeForum_DiscussionComments" lazy="false">
<id name="DiscussionCommentID">
<generator class="native" />
</id>
<property name="DiscussionID" />
<property name="DiscussionComment" />
<property name="CreatedBy" />
<property name="CreatedByIP" />
<property name="CreatedOn" />
<property name="UpdatedBy" />
<property name="UpdatedOn" />
</class>
</hibernate-mapping>
and here is the code I used to delete the children
Code:
....
JumptreeForum_DiscussionComments comment = (JumptreeForum_DiscussionComments)
ExampleApplication.GetCurrentSession()
.Load(typeof(JumptreeForum_DiscussionComments), Convert.ToInt32(discussionCommentId));
ExampleApplication.GetCurrentSession().Delete(comment);
ExampleApplication.GetCurrentSession().Flush();
Thanks