NHibernate: Entities Mapped Incorrectly Despite Correct Configuration

64 Views Asked by At

I'm facing an issue with NHibernate where entities are being mapped incorrectly despite what seems to be the correct configuration. Here's a summary of the scenario:

I have a class ProductPrices with two collections:

public virtual IList<ProductPrice_A> ProductPrice_A { get; set; }
public virtual IList<ProductPrice_B> ProductPrice_B { get; set; }

In the mapping for ProductPrices, I've defined the collections as follows:

public class ProductPricesMapping : ClassMap<ProductPrices>
{
    public ProductPricesMapping()
    {
        // Other mappings omitted for brevity
        HasMany(x => x.ProductPrice_A).Cascade.AllDeleteOrphan().Inverse();
        HasMany(x => x.ProductPrice_B).Cascade.AllDeleteOrphan().Inverse();
        // Other mappings omitted for brevity
    }
}

Additionally, there are subclasses ProductPrice_A and ProductPrice_B with their respective mappings, which include discriminator values and other properties.

The issue arises when querying entities of type ProductPrices. Despite the mappings being configured correctly, all entities are being added to the ProductPrice_A collection, even though some should belong to the ProductPrice_B collection.

I've already verified the data in the database, and everything appears to be correct. The discriminator values and associations seem to align with the NHibernate mappings.

Here are the additional mappings for reference:

public class ProductPrice_AMapping : SubclassMap<ProductPrice_A>
{
    public ProductPrice_AMapping()
    {
        Map(x => x.Description).Not.Nullable();

        DiscriminatorValue(1);
    }
}

public class ProductPrice_BMapping : SubclassMap<ProductPrice_B>
{
    public ProductPrice_BMapping()
    {
        Map(x => x.Description).Not.Nullable();

        DiscriminatorValue(0);
    }
}

public class ProductPriceMapping : ClassMap<ProductPrice>
{
    public ProductPriceMapping()
    {
        DiscriminateSubClassesOnColumn("discriminator");

        Id(x => x.Id).Not.Nullable();
        Map(x => x.CreateDate).Not.Nullable();
        Map(x => x.ModificationDate).Not.Nullable();
        Map(x => x.Discriminator).Nullable();

        References(x => x.ProductPrices).Not.Nullable().Cascade.SaveUpdate();
        Map(x => x.Price).Nullable();
    }
}

I'm puzzled as to why NHibernate isn't correctly associating entities with the ProductPrice_B collection and would appreciate any insights or suggestions on how to troubleshoot and resolve this issue.

1

There are 1 best solutions below

0
Roman Artiukhin On

Looks like the following issue: https://github.com/nhibernate/nhibernate-core/issues/1248

As a workaround you can force adding discriminator value to queries by adding AlwaysSelectWithValue() on discriminator mapping (force="true" attribute in xml mappings):

  public ProductPriceMapping()
    {
        DiscriminateSubClassesOnColumn("discriminator")
               .AlwaysSelectWithValue();

...