0
votes

I recently ran into a problem with using nhibernate mapping dictionaries to particular relation model. The OO side include entities as follows

class B // base class
{
    public long Id;
    public string Name;
    public Owner; // a reference to the owner group
}

class Group : B
{
    public IDictionary<string, B> ObjectsInGroup; 
        // dictionary for a look-up from name of any object contained in the group 
        // to the object itself
}

and I wanted to use the following tables to persist the above entities

column definition of table B for holding entities of type B:

  • Id: bigint (primary key)
  • Name: nvarchar(50)
  • Owner: bigint (related to Id as foreign key)

column definition of table G for holding entities of type Group:

  • Id: bigint (primary key)

As the objects in the group are managed in a dictionary, and dictionary based mapping of nhibernate is intended to be used, I suppose the actual table on which the mapping is based is extracted from table B. as the standard table underpinning a dictionary mapping is in the following form:

Id (of the dictionary provider) | Key | Value

where each line represents a key-value pair in the dictionary of entity with identifier 'Id'.

So basically the imaginary table in such a form to enable the mapping can be created by following SQL (just to give some idea or clarify the question):

select g.Id, o.Name, o.Id from G g, B o where o.Owner = g.Id

This is somehow a simplified model, the original one might have a separate class for leaf objects which may have an additional table involved making the problem even worse.

Not sure whether NHibernate actually supports such a mapping or if there some some workarounds or a need to redesign the model. Any thoughts are appreciated.

1

1 Answers

0
votes

I think the issue here is not the mapping but the model.
Logically, I think having a collection of Bs in your Group class would make more sense.
(IEnumerable<B> ObjectsInGroup)
Searching by an object's name can be done easily with LINQ ObjectsInGroup.Where(b => b.Name == "blah");.
That way your mapping is much simpler, and your class is more understandable.