Fluent | Nhibernate multiple inheritance mapping?

Posted by Broken Pipe on Stack Overflow See other posts from Stack Overflow or by Broken Pipe
Published on 2011-02-16T11:09:12Z Indexed on 2011/02/16 15:25 UTC
Read the original article Hit count: 481

I'm trying to map this classes:

public interface IBusinessObject
{
    Guid Id { get; set; }
}
public class Product
{
    public virtual Guid Id { get; set; }
    public virtual int ProductTypeId { get; set; }
}
public class ProductWeSell : Product, IBusinessObject
{
}
public class ProductWeDontSell : Product
{
}

Using this Fluent mapping code:

public class IBusinessObjectMap : ClassMap<IBusinessObject>
{
    public IBusinessObjectMap()
    {
        Id(t => t.Id).GeneratedBy.Guid();
        Table("BusinessObject");
    }
}
public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(t => t.Id);
        DiscriminateSubClassesOnColumn("ProductTypeId", "null").Nullable();
    }
}
public class ProductWeSellMap : SubclassMap<ProductWeSell>
{
    public ProductWeSellMap()
    {
        DiscriminatorValue(1);
        KeyColumn("Id");
    }
}
public class ProductWeDontSellMap : SubclassMap<ProductWeDontSell>
{
    public ProductWeDontSellMap()
    {
        DiscriminatorValue(2);
        KeyColumn("Id");
    }
}

But I get {"Duplicate class/entity mapping ProductWeSell"} error. And if we take a look at generated HBM, indeed it's duplicated, but i have no idea how to write this mapping without duplicating it if it's possible at all.

Produced hbm:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="IBusinessObject" table="BusinessObject">
    <joined-subclass name="ProductWeSell" table="ProductWeSell"/>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" discriminator-value="null" name="Product" table="Product">
    <discriminator type="String">
      <column name="ProductTypeId" not-null="false" />
    </discriminator>
    <subclass name="ProductWeDontSell" discriminator-value="2" />
    <subclass name="ProductWeSell" discriminator-value="1" />
  </class>
</hibernate-mapping>

So far I was unable to figure out how to map this using fluent Nhibernate (i haven't tried mapping this using hmb files). Any help appreciated Fluent or HBM files.

The thing I'm trying to solve look identical to this topic: NHibernate inheritance mapping question

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate