Question

I have three entity where Email & Sms entities is inherited of Message entity

public class Message 
{
    virtual public long Id { get; set; }
    virtual public string Body { get; set; }
}

public class Email : Message 
{
    virtual public string Subject { get; set; }
    virtual public string ToAddress { get; set; }
}

public class Sms : Message 
{
    virtual public string Number{ get; set; }
}

Mapping :

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core.Domain" namespace="Core.Domain.Model">
  <class name="Message" table="Core_Message"  >

    <id name="Id" >
      <generator class="native" />
    </id>

    <version name="Version"/>

    <property name="Body" not-null="true"/>   

  </class>
</hibernate-mapping>

,

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core.Domain" namespace="Core.Domain.Model">
  <union-subclass name="Email" extends="Message" table="Core_Email"  >

    <property name="Subject"/>

    <property name="ToAddress"/>

  </union-subclass>
</hibernate-mapping>

,

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core.Domain" namespace="Core.Domain.Model">
      <union-subclass name="Sms" extends="Message" table="Core_Sms"  >

        <property name="Number"/>   

      </union-subclass>
    </hibernate-mapping>

I have a exception by this message : Cannot use identity column key generation with <union-subclass> mapping for: Core.Domain.Model.Message

I need to what strategy for generate Id instead of native? I expect this values for Id is : 1,2,3,4,5,6,... that 1,3,5 are for Email and 2,4,6 are for Sms

Was it helpful?

Solution

The identifier must be unique over all the tables involved in the inheritance hierarchy. Therefore a generator that works separately for each table isn't allowed. Look in the NHibernate reference's section on generators for a list of (some) available algorithms. The hilo algorithm is common for numerical identities, though not necessarily the easiest to work with manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top