Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Problems with generated SQL #167
I'm having a bit of trouble with some SQL generated by my FluentNHibernate mappings. I'm not doing anything too complex, but unfortunately it's not behaving the way I would have expected.
I have a table-per-type hierarchy relating a ContentModule table to several sub-type tables. For the CategoryContentModule and EventContentModule types, there is an additional one-to-many relationship to the ContentItem table.
Here's the class definition for my tables; make note of the intermediary abstract type ContentItemContentModule:
Finally, my mappings:
As you can see, the intermediary type ContentItemContentModule is never mapped and exists only to provide the ContentItem property of its sub-types. In the mappings for each sub-type I explicitly map the ContentItem property.
When this mapping is registered and I attempt to retrieve a collection of ContentModule items via NHibernate, the generated SQL produces a non-existent column called ContentItemContentModuleId.
I've attempted explicitly setting the column name of the ContentItem reference to 'ContentItemId' (matching the table structure), but this did not work. After some experimentation I determined that referencing the ContentItem property was not the problem, but rather having the intermediary class ContentItemContentModule was. If I remove the intermediary abstract class and move the ContentItem property into each sub-type, the SQL is generated correctly and everything works.
The solution I came up with was simply to eliminate the intermediary abstract type and instead use an interface to achieve the same result in my code, but this is obviously a workaround.
TL;DR For some reason, FluentNHibernate is having problems with there being an unmapped abstract type between a mapped base abstract type and a mapped concrete type in a table-per-type hierarchy.