3

I am using Fluent and NHibernate. I have two objects say A & B which has a many-to-many relationship between them. I am using a unidirectional many-to-many mapping when A HasMany B's. There is no reference in B about A (Unidirectional).

This creates a third table (named ABMapping) in the Database which has the two columns relating to primary keys of A & B.

If I delete the object A, the entries from the ABMapping table related to A are deleted. That's cool.

But, now I am not able to delete an object B, as it has a FK constraint. How can I set it up so that on deleting B, all entries related to B in ABMapping are deleted automatically?

Zuber
  • 577
  • 1
  • 11
  • 29
  • Is the unidirectional constraint an important one? With it, at a fundamental level you would have to search over all of the mappings for ones related to B for each entry in B that is deleted. – Clueless Dec 30 '09 at 13:28
  • Yes the mapping is important. And I want to avoid the scenario where on deleting B I have to delete the mappings on B explicitly. Is that the only way to go? Also, what happens for unidirectional many-to-one? I think it will be the same scenario. If I delete B, I will have to search all objects of type A which refer to B, and delete them first before deleting object B. – Zuber Dec 30 '09 at 14:36

1 Answers1

1

If B doesn't reference A then it doesn't know about the mapping table so it can't cascade the delete. As I see it you have two options:

  1. Cascade the delete in the database using cascading deletes on your FK or a trigger.
  2. Map the relationship from B to A; you don't have to expose it to consumers of your class, the A collection could be mapped as a private field using an access strategy. I always do this for collections (using .Access.CamelCaseField(Prefix.Underscore)) so that I don't expose IList.
Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • Thanks for the help Jamie. One more question I now have is that if I use bidirectional mapping, and then delete an object of class B, it should only delete the mapping in the ABMapping table. However, when I tried the Cascade settings as .Delete or .DeleteOnCascade it goes and deletes the object A as well. I also had an Inverse set on the mapping in B. I tried Cascade.None() but i that case it did not delete the mapping in ABMapping and gave me the constraint violation error. – Zuber Dec 30 '09 at 17:50
  • @Zuber - My answer was incomplete. You shouldn't use NH cascade with many-to-many. You need to remove the B object from A's collection of Bs (this removes the links), then delete the B object. I think if you go this route you will not need to map the A collection in B but you'll probably want to so that you have a reference to the A objects that reference the B object. – Jamie Ide Dec 30 '09 at 18:09
  • @JamieIde what do you mean by "cascading deletes on your FK?" Do you mean that there is a way to tell Hibernate/JPA to delete all rows with FK references for an `@Entity` before deleting the row that uses the PK? (Because that would be awesome) If you just mean `Cascade.DELETE` on `B`'s reference to the `A`s, then that's not interesting because deleting a `B` would delete the `A` rows as well. – fommil Aug 13 '12 at 11:39