0
votes

I have two tables (Rule and Object) linked through a many-to-many relationship. A Rule may be associated with any number of Objects, or it may be associated with all Objects.

I would normally build this association with a link table where, the Object_ID column would be set to NULL if the associated Rule was to be associated with all Objects. Any value that won't actually reference a real Object will do.

This way, I could write an Select to find all Rules associated with an object like this:

SELECT * FROM Rule JOIN RuleObject_Link on Rule.ID = RuleObject_Link.RuleID WHERE RuleObject_Link.ObjectID = <the object ID> or RuleObject_Link.ObjectID IS NULL

The problem is that I am using NHibernate. I can't find a way to signify "all Objects" in the automated relationship/collection structure.

Is it possible to build a relationship like this using NHibernate's many-to-many relationship?

Or will I have to manually configure the link table and handle the connection myself?

1

1 Answers

0
votes

It would be better to add a separate boolean field to Rule signifying that it applies universally to all Object objects (Object is a pretty bad classname by the way).

This really doesn't participate in the relationship mapping, and getting the entire list of rules for an object would require a query rather than being populated automatically from mappings, but I can't think of anything better right now.