2

I have three table - 1. Anomaly 2. Markup 3. Anomaly_Markup

Mapping -

    public AnomalyMap()
    {
        Table("anomaly");

        Id(x => x.Id).Column("ID").CustomType("decimal");

        HasManyToMany<DMMarkupData>(x => x.DMMarkupData)
            .Table("anomaly_markup")
            .ParentKeyColumn("ANOMALY_ID")
            .ChildKeyColumn("MARK_UP_ID")
            .Cascade.All()
            .LazyLoad();
    }

    public MarkupDataMap()
    {
        Table("markup");
        Id(x => x.Id).Column("ID");
    }

Condition :

  1. Save data by Anomaly - Anomaly contains MarkupData. It saves data. It is working functionality with me.
  2. Delete markup - which should delete relationship from map table and markup data. I am facing this issue.

Anyone help me to find out solution, how to delete markup data ?

3 Answers3

0

From what you've posted it looks like you haven't defined a relationship on DMMarkupData -> Anomaly, so NHibernate won't know to delete it the MarkupData entries from the anomaly_markup table (despite the reverse relationship being there). You can either solve it with a database level cascade which removes entries in anomaly_markup when deleting MarkupData, or you can map the relationship in code & NHibernate and then NHibernate will do the cascade for you.

Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • 2
    Thanks Martin, when I put reverse relationship in MarkupDataMap also then duplicate value is inserted in anomaly_markup table. see this post - http://hackingon.net/post/Notes-on-NHibernate-Many-to-many-Relationships.aspx –  Oct 04 '12 at 04:32
  • You need to make the reverse relationship Inverse – Martin Ernst Oct 04 '12 at 09:10
  • 2
    Could you please describe little bit more ? Thanks for look into it. –  Oct 04 '12 at 13:27
  • In your HasManyToMany in the MarkupDataMap, you should be able to add .Inverse() which will mark the relationship as inverse - ie. that membership of the relationship is controlled by the Anomoly not by the MarkupData – Martin Ernst Oct 04 '12 at 17:49
0

I see no relationship of MarkUpData with Anomaly. There must be same relationship and you should specify the control of cascade operation by using Inverse attribute in your mapping. You can refer : How to set "cascade delete" option to "Set Null" in Fluent NHibernate?

To delete DMMarkupData just remove the object from collection and call for Save Anomaly.

Community
  • 1
  • 1
Deepak
  • 420
  • 5
  • 21
0

NHibernate does not manage the object graph, it only persists it. Removing the item from the list when it gets deleted is responsibility of the business logic!

(All the tricks with triggers and stuff that workaround it lead to inconsistencies and side effects within the transaction which does the change. From point of view of persistence ignorance it is not recommended. I would only do it when facing performance issues that can't be solved in another way.)

You can simplify it by using components. Provided that

  • you don't reference the same markup from somewhere else
  • you don't need to query for markup unrelated to Anomality
  • markups do never live outside of an Anomality

given all that, it is much easier to work with components. (I don't know how it is called in fluent, but in xml mapping it is called a "composite-element").

When using components, you don't need to delete the markup from the database. You just remove it from the list where it lives in.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193