views:

237

answers:

2

I have a class StoreHours that has a composite key and has been working perfectly. A new demand came up for another type of hours to be returned. I thought "simple, I'll abstract the base class, have two concrete implementations and change my references in the app to one of the new classes". However, upon doing that, my unit tests failed with

X.Test.StoreTest.HoursTest: NHibernate.InstantiationException : Cannot instantiate abstract class or interface: X.Model.StoreHours

My mapping file looks like

<class name="StoreHours" table="StoreHour" abstract="true" discriminator-value="0" >
 <composite-id>
  <key-many-to-one name="Store"
   class="Store"
   column="StoreUid"/>
  <key-property name="DayOfWeek" 
   column="DayOfWeekId"
   type="System.DayOfWeek" />
 </composite-id>
 <discriminator column="StoreHourType" type="Byte" />
 <property name="OpenMinutes" column="OpenTime" />
 <property name="CloseMinutes" column="CloseTime" />
 <subclass name="OfficeHours" discriminator-value="1" />
 <subclass name="AccessHours" discriminator-value="2" />
</class>

I found someone with similar troubles here and started down their solution path but actually ended up with even more troubles than I started with.

I can persist the records to the database perfectly but onload, NHibernate is trying to instantiate the abstract 'StoreHours' even though I've only got a strongly type set off 'OfficeHours'

This seems like a really trivial requirement so I figure I must be doing something simple wrong. All hints appreciated.

A: 

My guess is it's trying to instantiate StoreHours because that is the class name defined in that XML document.

Woot4Moo
OfficeHours and AccessHours are also defined there though and StoreHours is defined as abstract. I have other subclass functionality that works like this, the only difference appears to be the composite-id...
toxaq
Is the composite ID necessary? My thought is it isn't, so I would personally refactor my code to exclude the composite id
Woot4Moo
Yes, it's a join between the non-exist DayOfWeek table (an enum in .Net) and the Store table.
toxaq
A: 

So I ended up splitting the StoreHour table into two, OfficeHour and AccessHour. Seems crazy to me that I had to alter my schema to make ORM work in a trivial case and create duplicate database tables for two identical objects. ORM = Object Relational Mutilation?

Still open to other real answers.

toxaq
In my experience hibernate is overkill in 95% of cases, however it does not take away from the fact that it is popular
Woot4Moo