How to model a relationship that NHibernate (or Hibernate) doesn’t easily support

Posted by MylesRip on Stack Overflow See other posts from Stack Overflow or by MylesRip
Published on 2010-03-26T20:00:47Z Indexed on 2010/03/26 20:03 UTC
Read the original article Hit count: 166

I have a situation in which the ideal relationship, I believe, would involve Value Object Inheritance. This is unfortunately not supported in NHibernate so any solution I come up with will be less than perfect.

Let’s say that:

  • “Item” entities have a “Location” that can be in one of multiple different formats.
  • These formats are completely different with no overlapping fields.
  • We will deal with each Location in the format that is provided in the data with no attempt to convert from one format to another.
  • Each Item has exactly one Location.
  • “SpecialItem” is a subtype of Item, however, that is unique in that it has exactly two Locations.
  • “Group” entities aggregate Items.
  • “LocationGroup” is as subtype of Group.
  • LocationGroup also has a single Location that can be in any of the formats as described above.
  • Although I’m interested in Items by Group, I’m also interested in being able to find all items with the same Location, regardless of which group they are in.

I apologize for the number of stipulations listed above, but I’m afraid that simplifying it any further wouldn’t really reflect the difficulties of the situation. Here is how the above could be diagrammed:

Mapping Dilemma Diagram: (http://www.freeimagehosting.net/uploads/592ad48b1a.jpg)
(I tried placing the diagram inline, but Stack Overflow won't allow that until I have accumulated more points. I understand the reasoning behind it, but it is a bit inconvenient for now.)
Hmmm... Apparently I can't have multiple links either. :-(

Analyzing the above, I make the following observations:

  • I treat Locations polymorphically, referring to the supertype rather than the subtype.
  • Logically, Locations should be “Value Objects” rather than entities since it is meaningless to differentiate between two Location objects that have all the same values. Thus equality between Locations should be based on field comparisons, not identifiers. Also, value objects should be immutable and shared references should not be allowed.
  • Using NHibernate (or Hibernate) one would typically map value objects using the “component” keyword which would cause the fields of the class to be mapped directly into the database table that represents the containing class. Put another way, there would not be a separate “Locations” table in the database (and Locations would therefore have no identifiers).
  • NHibernate (or Hibernate) do not currently support inheritance for value objects.

My choices as I see them are:

  1. Ignore the fact that Locations should be value objects and map them as entities. This would take care of the inheritance mapping issues since NHibernate supports entity inheritance. The downside is that I then have to deal with aliasing issues. (Meaning that if multiple objects share a reference to the same Location, then changing values for one object’s Location would cause the location to change for other objects that share the reference the same Location record.) I want to avoid this if possible. Another downside is that entities are typically compared by their IDs. This would mean that two Location objects would be considered not equal even if the values of all their fields are the same. This would be invalid and unacceptable from the business perspective.
  2. Flatten Locations into a single class so that there are no longer inheritance relationships for Locations. This would allow Locations to be treated as value objects which could easily be handled by using “component” mapping in NHibernate. The downside in this case would be that the domain model becomes weaker, more fragile and less maintainable.
  3. Do some “creative” mapping in the hbm files in order to force Location fields to be mapped into the containing entities’ tables without using the “component” keyword. This approach is described by Colin Jack here. My situation is more complicated than the one he describes due to the fact that SpecialItem has a second Location and the fact that a different entity, LocatedGroup, also has Locations. I could probably get it to work, but the mappings would be non-intuitive and therefore hard to understand and maintain by other developers in the future. Also, I suspect that these tricky mappings would likely not be possible using Fluent NHibernate so I would use the advantages of using that tool, at least in that situation.

Surely others out there have run into similar situations. I’m hoping someone who has “been there, done that” can share some wisdom. :-)

So here’s the question… Which approach should be preferred in this situation? Why?

© Stack Overflow or respective owner

Related posts about domain-driven-design

Related posts about nhibernate