Nhibernate fires SQL commands

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-02-17T09:55:30Z Indexed on 2010/04/15 9:03 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

Hi all,

when updating an entity A, NHibernate also send an SQL update command for some other entity B. A and B are not related. Just before saving entity A, the parent of entity B is loaded via a SQLQuery. Then, when accessed, B is lazy loaded (part of a collection). If I save entity A an update statement for entity B is generated as well.

How can that be, that when saving an entity, another entity loaded before but is not related to the entity saved, is updated as well?!

Can I somehow track where the update comes from?

Btw. I am using an save event listener. Could it be that this is always triggered for entity loaded, even though they are not saved explicitly?

public class EntitySaveEventListener : NHibernate.Event.Default.DefaultSaveEventListener
{
    protected override object PerformSaveOrUpdate(SaveOrUpdateEvent e)
    {
        //auditing

        return base.PerformSaveOrUpdate(e);
    }

}

Update (sorry for providing not enough info):

I tracked it down a bit. A select stateement on a entity called address is executed (is it lazy loaded by a parent).

Then I create a new entity called Request. Right before saving this entity a session flush is called which updates the address, even though I did not call save or update on the address. Address is a collection within Request.

  <class name="Request" table="Request">
    <bag name="addresses" access="field" cascade="all-delete-orphan" where="IsDeleted = 0">
      <key column="RequestId"/>
      <one-to-many class="Address"/>
    </bag>
   ...

// address is fetched only
NHibernate.SQL: 2010-02-17 11:47:21,306 [21] DEBUG NHibernate.SQL [(null)] - SELECT addresses0_.RequestId as ServiceP8_3_, ....

// session flushed here

// address is updated
NHibernate.SQL: 2010-02-17 11:47:34,306 [21] DEBUG NHibernate.SQL [(null)] - Batch commands:
command 0:UPDATE Address SET Street = @p0, .....

Would the address be updated automatically when it is manipulated somehow even though it is not explicitly saved via it's parent (cascade)?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about nhibernate