nhibernateinterfacenhibernate-mappingtable-per-hierarchydiscriminator

Persisting one table per class hierarchy using NHibernate without discriminator?


I have an interface and a class which implements this interface.

public interface IPhase {
    string Description { get; set; }
    int Id { get; }
    string Phase { get; set; }
}

public class Phase : IPhase {
    // Implementation here...
}

Now, using NHibernate 2.1.2.GA, I wish to use a table-per-class-hierarchy to map the interface and its implementor, and indeed, I don't need a discriminator, as this implementor will be the only class persisted in this table. I haven't used NHibernate for about a year and a half now, and I'm suffering some memory blanks here...

I have read this question and answers which is related, except I'm not using FNH.
NHibernate Mapping: Save hierarchy to single table without discriminator

  1. I wonder whether the discriminator attribute is obligatory while using subclass?

  2. What shall my XML mapping look like in this particular context?

Thanks kindly for your help!


Solution

  • Of course NHibernate needs a discriminator for a table-per-class-hierarchy mapping, how should it identify the different subclasses when getting a row from the database otherwise?

    If there is only one implementation of the interface, why do you want to map it? Just map the class as a normal entity (without inheritance).

    edit: Forgot that you might have references to the interface in your mapping. In that case you could try a table-per-concrete-class mapping marking the base class (interface) with abstract="true" like described here: http://nhibernate.info/doc/nh/en/index.html#inheritance-tableperconcrete