Multi Table Entities in NHibernate
A while ago I posted about the ability to map n tables to a single entity in the Entity Framework. I didn't like it then, and I quoted from the Hibernate documentation that discourage this behavior:
I still believe that this statement is true, except... I just run into an issue with my model, I have a case where I am importing data from another database, and I need to add additional data to it. I could add additional columns to the primary table, but that would make the import process much more complex than I would like it to be. I would have liked to make it work by splitting the data by table, rather than by columns.
With that in mind, I headed to NHibernate's JIRA, and found this issue about the problem. Conveniently, a patch was supplied as well.
A big thanks for Karl Chu for making all the work of porting the functionality from Hibernate. I love Open Source.
At any rate, you can now map several tables into a single entity in NHibernate, you can get the full details here (the new tests), but let us walk through a simple one first.
Name and sex are defined in the Person table, but everything else is defined on the Address table, we map it like this:
<id name="Id" column="person_id" unsaved-value="0">
Obviously address_id is a FK to person_id (not the best names for them, come to think about it). Trying to load a person would cause this SQL query (reformatted):
FROM dbo.Person p inner join dbo.Address a
There is quite a bit more that it can do (optional joins, etc), and you can check it out at the tests.
Note: this is on NHibernate trunk, so it won't be in the 1.2 release, which is currently in feature-freeze.