Hi guys,
I have this problem with collection when deleting an entity. I think should be 
something worng in the way cascade is set.. but can not figure it out.
The problem is when calling booking.RemoveComment( comment )  nothing gets 
deleted. Actually the ISet Remove method does not remove the comment from
the collection.
If anyone could help me out on what I'm doing wrong here ...

Mappings

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" 
assembly="ISTWebCore" namespace="IST.Web.Core">
  <class name="Booking" table="bookings">
    <id name="Bkid" column="bkid" type="int">
      <generator class="native">
        <param name="sequense">bookings_bkid_seq</param>
      </generator>
    </id>
    ....
    <set name="Comments" inverse="true" lazy="true" cascade="all-delete-orphan" 
access="field.camelcase-underscore">
      <key column="bkid"/>
      <one-to-many class="Comment"/>
    </set>  
   </class>
  </hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ISTWebCore" 
namespace="IST.Web.Core">
  <class name="Comment" table="comments">
    <id name="Cmid" column="cmid"  type="int" unsaved-value="0" 
access="nosetter.camelcase-underscore">
      <generator class="native">
        <param name="sequense">comments_cmid_seq</param>
      </generator>
    </id>
    .....
    <many-to-one name="Bkid" column="bkid" class="Booking" />
    <many-to-one name="Usid" column="usid" class="User" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ISTWebCore" 
namespace="IST.Web.Core">
    <class name="User" table="users">
        <id name="Usid" column="usid" type="int">
            <generator class="native">
                <param name="sequense">users_usid_seq</param>
            </generator>
        </id>        
       ...........
       <set name="Comments" inverse="true" lazy="true" 
cascade="all-delete-orphan" access="field.camelcase-underscore">
        <key column="usid"/>
        <one-to-many class="Comment"/>
       </set>
      
    </class>
</hibernate-mapping>


Entities

public class Booking
{
    public virtual int Bkid { get; set; }
    ............
    
    private readonly ISet<Comment> _comments = new HashedSet<Comment>();

    public virtual ReadOnlyCollection<Comment> Comments { get { return new 
ReadOnlyCollection<Comment>(_comments.ToList()); } }

    public virtual bool RemoveComment(Comment commentToRemove)
    {
       if (commentToRemove != null &&
          _comments.Remove(commentToRemove))
       {
           commentToRemove.SetBooking(null);
          return true;
       }
        return false;
    }

      ........

}

public class Comment
{
    public virtual int Cmid { get { return _cmid; } }
    public virtual Booking Bkid { get; set; }

    public virtual User Usid { get; set; }

    public virtual void SetBooking(Booking newBooking)
    {

        var prevBooking = Bkid;

        if (newBooking == prevBooking) return;

        Bkid = newBooking;

       if (prevBooking != null) prevBooking.RemoveComment(this);

      if (newBooking != null) newBooking.AddComment(this);

   }

   ..................
}

public class User
{
     public virtual int Usid { get; set; }
     
    private readonly ISet<Comment> _comments = new HashedSet<Comment>();

    public virtual ReadOnlyCollection<Comment> Comments { get { return new 
ReadOnlyCollection<Comment>(_comments.ToList()); } }


}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to