views:

447

answers:

2

My object model represents a podcast feed, with channel data and feed items (individual podcasts). This is structured as follows:

PodcastFeed
    ChannelData // Property on PodcastFeed
        ITunesChannelData // Property on ChannelData
    FeedItems // Property on PodcastFeed; collection of PodcastFeedItems

PodcastFeedItem
    ITunesItemData // Property on PodcastFeedItem

In my database, the fields on PodcastFeed, ChannelData and ITunesChannelData are all stored in the one table; equally the fields on PodcastFeedItem and ITunesItemData are all stored in a single table. There's a one-to-many relationship from PodcastFeed to PodcastFeedItem. The reason for storing them this way is because there is a one-to-one mapping between the different object types (e.g., the ChannelData is unique to each podcast feed).

I'm ok with the mapping of PodcastFeed to PodcastFeedItem, but what I can't work out how to complete is how to "partition" the data in the Feeds table across the different classes making up the PodcastFeed. I have successfully created NHibernate mappings from the Feeds table to these types, but when I access a PodcastFeed object, the ChannelData property is null. Presumably this is because NHibernate doesn't know to create the hierarchy as described above.

FWIW, I've tried the following line in my mapping file with no success (NHibernate tries to load data from the non-existent "ChannelData" column on the table).

<property name="ChannelData" type="ChannelData" />

Do I need to create a user type in NHibernate to enable this, or am I missing some built-in trick?

A: 

What your looking for is TPH, Table-per-Hierarchy, mapping. This is where you have a single table in a database mapped to a hierarchy of objects, differentiated by some kind of key.

The following documentation should e helpful: https://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html

jrista
My "diagram" doesn't describe an object inheritance hierarchy, it describes an object with other object members (hence the "Property on..." comments). The link was helpful, though; I was actually after Component Mapping, which happened to be the previous chapter.
alastairs
Ah, my apologies...I somehow didn't notice the glaring "Property" comments all over the place :P
jrista
+1  A: 

The way to solve this is to use component mapping. The <component /> mapping element will allow for as many types of mapping as are available at the <class /> level, allowing for complex scenarios.

alastairs