-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: NHibernate trying to update deleted entity
PostPosted: Thu Jun 28, 2007 6:54 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Hibernate version: 1.2.0.GA

I am trying to delete an entity which is in a child collection of another entity. For example, I am trying to delete a ContactAddress entity, which is a member of a Contact entity's Addresses collection.

I do this:

Code:

// At this point, myContactAddress.Contact = myContact

// We've set this up so that removing an entity from a collection
// automatically nulls out the appropriate "parent" property;
// in this case, it's ContactAddress.Contact
myContact.Addresses.Remove(myContactAddress);

// At this point, myContactAddress.Contact = null

mySession.Delete(myContactAddress);


This executes OK, but when the session is flushed, I get a SQL error about NHibernate trying to update the contact address row with a null value for the contact ID foreign key column. Buty since I called ISession.Delete() on myContactAddress, it shouldn't consider it for updating (or re-inserting) even if it appears "dirty". Doesn't it know it's marked as deleted? This sounds like a bug to me ...

We really don't want to disable the automatic maintenance of parent property values in our collections when items are added and removed, so it isn't an option on our end to try working around it by preventing the entity being deleted from getting "dirty". (If we don't remove it from the parent's collection, we get an exception that the deleted entity would be resaved by an update ...)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 7:12 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Since we track our own deleted status (and dirty status) on our entities, I was able to work around it by implementing IInterceptor.FindDirty like this:

Code:
public int[] FindDirty(
    object   entity,
    object   id,
    object[] currentState,
    object[] previousState,
    string[] propertyNames,
    NH.Type.IType[] types)
{
    MyCompany.Entities.IEntity ent = (MyCompany.Entities.IEntity)entity;

    if (ent.IsDeleted)
    {
        // Don't try to update entities marked deleted,
        // even if they appear dirty via the default algorithm.
        // Seems like an NHibernate bug that it would consider it for update ...

        return new int[0]; // empty array indicates that the entity is not dirty
    }

    return null; // null indicates to use NHibernate's default dirty-checking algorithm
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 29, 2007 1:13 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate considers collection updates separately from updates to contained objects, this may be why you see this problem occurring. You should be able to prevent it using inverse="true" for the Addresses collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 7:30 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
All of our collections are already mapped as inverse=true. They are all mapped as bags.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 9:51 am 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
Hi. I have the same problem. The difference is I have 2 bags point to same entity, like a many to many. I didn't use many-to-many because the entity reserve has more fields besides the keys.

Client [bag]-> reserve <-[bag] Item

I tried to use inverse in both sides and no inverse too. In all cases NHibernate tries to update one of the keys in reserve with null value and fails.

The only way I made it work was mapping both bags with inverse and delete de reserve myself. I don't know if it is the better choice.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.