2

I'm sending a JSON collection from Javascript through a REST web service to be Deserialized using Json.NET and then finally updated in the DB using NHibernate (I'm using Fluent). The settings I'm using for Json.NET Deserialization are:

        JsonConvert.DeserializeObject<T>(jsonString,
        new JsonSerializerSettings { 
            PreserveReferencesHandling = PreserveReferencesHandling.Objects
        });

My Json object is:

{
  "ID": 1,
  "Name": "ObjectName",
  "Keys": [
    {
      "ID": 6,
      "Name": "ID"
    }
  ]
}

Modifying properties of the object, and even adding and modifying the child objects (Keys) and then deserializing works great. However, i can't seem to work out how to manage child object deletes. For example, if I remove a child object (using Object.Keys.splice), and then update, the DB record it relates to remains persistent even though the parent object which is updated does not contain them.

Additionally, I have also set 'AllDeleteOrphan' in the mapping file.

    // one-to-many
    HasMany(x => x.Keys)
      .Inverse()
      .Cascade.AllDeleteOrphan();

Is there any way to ensure that child objects removed via javascript will be cascade deleted when the object is deserialized and updated?

I have also read that when using NHibernate, child objects need to be removed manually from the parent collection, via Keys.Remove(item), before update. It would be much more preferable (not to mention scalable) to just deserialize and update. Any chance?

1 Answer 1

2

You should be able to update and then see those changes cascade, but I believe the issue has to do with the collection being marked as inverse.

Inverse=true (which is fluent nhibernate's .Inverse() is binding) is like saying "I do not manage this relationship". Due to this binding, the parent object is not deleting/updating the collection because the children manage the relationship.

Whether this makes sense or not, I could not really say without a fuller understanding of your domain.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks for getting back to me. I tried removing that, and still have the issue with deletes. Confusing, as I always thoughts that the Inverse tag specified which side of the relationship was responsible for saving (the record), not the relationship itself?
Hold everything - removing Inverse() from the mapping had the effect of nulling the foreign key in the DB - effectively ending the relationship.. so, it worked! Just need to remove the orphans and we have a win. Thanks.
Hi, Did you ever work out how to get it to remove the record rather than nulling the foreign key?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.