0

I faced with following case: I have table per class hierarchy:

public abstract class Parent : BaseEntity, IHierarchyEntity
    {
    }  

public class ChildA : Parent
{
    public virtual string Name { get; set; }
}

public class ChildB : Parent
{
    public virtual string Value { get; set; }
}

public class Container : BaseEntity
{
    public Container()
    {
        CollectionOne = new HashSet<ChildA>();
        CollectionTwo = new HashSet<ChildB>();
    }

    public virtual ICollection<ChildA> CollectionOne { get; set; }

    public virtual ICollection<ChildB> CollectionTwo { get; set; }
}

Small piece of Domain mapper logic (it`s almost the same):

IEnumerable<Type> allPersistEntities = GetDomainEntities();
IEnumerable<Type> roots = allPersistEntities.Where(t => t.IsAbstract && t.InheritedFromBaseEntity());
IEnumerable<Type> hierarchyEntities = allPersistEntities.Where(t => typeof(IHierarchyEntity).IsAssignableFrom(t));
var hierarchyRoots = hierarchyEntities.Where(t => t.IsAbstract && t.InheritedFromBaseEntity());
orm.TablePerClassHierarchy(hierarchyRoots);

When I saved items everything is ok, but when I tried to get ones I get two of them in the CollectionOne (ChildA type) and error in the second one:

illegal access to loading collection What I see in the sql:

NHibernate:
    SELECT
        container0_.Id as Id0_0_
    FROM
        CONTAINERS container0_
    WHERE
        container0_.Id=@p0;
    @p0 = 1 [Type: Int32 (0)] NHibernate:
    SELECT
        collection0_.ContainerId as Containe5_1_,
        collection0_.Id as Id1_,
        collection0_.Id as Id1_0_,
        collection0_.Name as Name1_0_
    FROM
        PARENTS collection0_
    WHERE
        collection0_.ContainerId=@p0;
    @p0 = 1 [Type: Int32 (0)] NHibernate:
    SELECT
        collection0_.ContainerId as Containe5_1_,
        collection0_.Id as Id1_,
        collection0_.Id as Id1_0_,
        collection0_.[Value] as Value3_1_0_
    FROM
        PARENTS collection0_
    WHERE
        collection0_.ContainerId=@p0;
    @p0 = 1 [Type: Int32 (0)]

There is no discriminator field. Is it possible to fix it?

h.alex
  • 902
  • 1
  • 8
  • 31
Ivan Korytin
  • 1,832
  • 1
  • 27
  • 41

1 Answers1

1

Can you try:

var hierarchyRoots = hierarchyEntities.Except(roots);

I think you might need to give it the exact leafs to map, I don't think it'll assume it should map all classes inheriting from the root as tpch.

h.alex
  • 902
  • 1
  • 8
  • 31
  • After that I`ve got an error: Could not determine type for: Dlls.ChildA, Dlls, for columns: NHibernate.Mapping.Column(CollectionOneElement) Looks like Nhibernate can`t clarify what type it is. – Ivan Korytin Sep 26 '17 at 06:02
  • I think it either didn't figure out the mapping for the FK relationship, or it didn't actually map the classes. Is this when saving or reading? Conform is able to produce an xml copy of the mapping it creates. Try producing that and comparing to what it should be - should give you an idea of what you need to correct. – h.alex Sep 27 '17 at 07:22