Hi guys.
I'm having a strange behavior with nhibernate. The problem is that
nhibernate performs an update before it deletes an entity.
I have a Category class and a Product class. Category has a bag of
products. When I remove a product from Category, nhibernate does the
following:
- It updates the product entity which I removed from collection
- It deletes the product entity from database.

Here's mapping

  <class name="Category">
    <id name="Id">
      <generator class="hilo" />
    </id>
    <property name="Name" lazy="false" length="20" />

    <bag name="Products" cascade="all-delete-orphan" lazy="false"
inverse="false">
      <key column="CategoryId" />
      <one-to-many class="Product" />
    </bag>
  </class>


  <class name="Product">
    <id name="Id">
      <generator class="hilo" />
    </id>
    <property name="Name" lazy="false" />
    <property name="Discontinued" lazy="false" />
    <property name="Price" lazy="false" />
    <many-to-one name="Category"
             class="Category"
             column="CategoryId"
             cascade="none" />
  </class>

Here's the code
            using (var session = NHibernateHelper.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                var c1 = session.Load<Category>(32768);
                c1.Ps.RemoveAt(0);

                session.SaveOrUpdate(c1);
                transaction.Commit();
            }

and here's the result:
exec sp_executesql N'UPDATE Product SET CategoryId = null WHERE
CategoryId = @p0 AND Id = @p1',N'@p0 int,@p1 int',@p0=32768,@p1=65537
go

exec sp_executesql N'DELETE FROM Product WHERE Id = @p0',N'@p0
int',@p0=65537
go

Anyone can explain this strange behavior?

Thanks.

-- 
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