I'm testing some nhibernate mappings to reduce the queries sent to the 
database and optimize the performance of an application.
I was testing the delete of an entity with an inverse one to many relation 
with cascade delete in the database and I have noticed that nhibernate load 
the children of the object before deleting it.
 
var list = Session.Query<List>().Single(x => x.Name == "Test");
//exec sp_executesql N'select list0_.Id as Id1_, list0_.Name as Name1_ from 
[List] list0_ where list0_.Name=@p0',N'@p0 nvarchar(4000)',@p0=N'Test'
Session.Delete(list); //What? It loads the Items collection from the 
database? Why?
//exec sp_executesql N'SELECT items0_.List_id as List3_1_, items0_.Id as 
Id1_, items0_.Id as Id0_0_, items0_.Name as Name0_0_, items0_.List_id as 
List3_0_0_ FROM [ListItem] items0_ WHERE 
items0_.List_id=@p0',N'@p0uniqueidentifier',@p0='74C0D7DE-3C36-4394-AD45-A21200CAA5E1'
Session.Flush();
//exec sp_executesql N'DELETE FROM [List] WHERE Id = @p0',N'@p0 
uniqueidentifier',@p0='74C0D7DE-3C36-4394-AD45-A21200CAA5E1'
 
I can imagine, that this is a side effect of the collection proxy, 
somewhere in the code, when an object is deleted, nhibernate check all the 
collections of the object, and this trigger the load of the collection from 
the database. I can imagine that the check is made to delete also the 
children from the database, but this check should be skipped for the 
collections with a cascade delete in the database, it generates only a 
useless query to the database. That could be also a performance problem, 
when an object as many big collections, load all the children from the 
database can be quite expensive operation.
 
The mapping of the List and ListItem are:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" 
name="ConsoleApplication5.Entities.List, ConsoleApplication5, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`List`">
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089" 
unsaved-value="00000000-0000-0000-0000-000000000000">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <bag cascade="all-delete-orphan" inverse="true" name="Items">
      <key on-delete="cascade" not-null="true">
        <column name="List_id" />
      </key>
      <one-to-many class="ConsoleApplication5.Entities.ListItem, 
ConsoleApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
/>
    </bag>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" length="50" not-null="true" unique-key="IX_List" 
/>
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" 
name="ConsoleApplication5.Entities.ListItem, ConsoleApplication5, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`ListItem`">
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089" 
unsaved-value="00000000-0000-0000-0000-000000000000">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" length="50" not-null="true" 
unique-key="IX_ListItem" />
    </property>
    <many-to-one cascade="none" class="ConsoleApplication5.Entities.List, 
ConsoleApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
foreign-key="FK_ListItem_List" name="List">
      <column name="List_id" not-null="true" />
    </many-to-one>
  </class>
</hibernate-mapping>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to