0

My data model looks like this.

public abstract class BasePivotField
    {
        public virtual int SystemId { get; set; }
        public virtual int Version { get; set; }
        public virtual string FieldName { get; set; }
        public virtual string FieldValuePrefix { get; set; }
        public virtual Field Field { get; set; }
        public abstract PivotFieldType FieldType { get; }
    }

public class PivotColumnField : BasePivotField
{
    public virtual CriteriaWithField FieldCriteria { get; set; }
    public virtual int Width { get; set; }

    public override PivotFieldType FieldType
    {
        get { return PivotFieldType.ColumnField; }
    }
}
public class PivotTableControl : BaseControl
{
        public virtual Criteria Criteria { get; set; }
        public virtual int Height { get; set; }
        public virtual PagingProperties PagingProperties { get; set; }
        public virtual IList<BasePivotField> PivotTableFields { get; set; } 
}

I use Fluent NHibernate mapping and the mapping classes are below

public class PivotTableControlMap : SubclassMap<PivotTableControl>
    {
        public PivotTableControlMap()
        {
            References(x => x.Criteria).Cascade.All().Column("PivotTableCriteria");
            Map(x => x.Height);
            Component(x => x.PagingProperties);
            HasMany(x => x.PivotTableFields)
                .KeyColumn("PivotTableId")
                .Cascade.AllDeleteOrphan()
                .AsBag();
        }
    }

public class PivotTableFieldMap : ClassMap<BasePivotField>
    {
        public PivotTableFieldMap()
        {
            Table("PivotTableField");
            Id(x => x.SystemId);
            Version(x => x.Version);
            Map(x => x.FieldName);
            References(x => x.Field).Cascade.All().Column("FieldId");
            Map(x => x.FieldValuePrefix);
            DiscriminateSubClassesOnColumn("PivotFieldType");
        }
    }

public class PivotColumnFieldMap : SubclassMap<PivotColumnField>
{
    public PivotColumnFieldMap()
    {
        Component(x => x.FieldCriteria).ColumnPrefix("PivotTable");
        Map(x => x.Width);
    }
}

There are other classes that inherit from BasePivotField, but I have left them out for brevity.

The problem is that when I load the PivotTableControls from the database using NHibernate, the PivotTableFields collection does not get loaded. The exception thrown says 'Creating a proxy instance failed'. Looking into the exception deeper, I can see that the PivotTableFields says 'illegal access to loading collection'. I can confirm that the Session is not closed. If I don't use lazy load, the problem still persists.

I have used a similar kind of model and mapping in other places and they work fine. What am I doing wrong here? Any clues will be highly appreciated!

Zuber
  • 577
  • 1
  • 11
  • 29

1 Answers1

0

I'm not sure if it resolve the issue but anyway you need add DiscriminatorValue(x => x.FieldType ) in PivotColumnFieldMap

Denis Borovnev
  • 486
  • 3
  • 3
  • I haven't had to do that for any other. I think DiscriminatorValue is for the Table Per Class approach. I am using a single table for all types and the Discriminator column is the PivotFieldType. – Zuber Sep 05 '12 at 14:09
  • Could you look at table-per-class-hierarchy strategy sample in this article https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping . I don't know what is saved in "Discriminate" column by default if this value not specified in subclass map (maybe type of subclass). Sorry if I was wrong that you need to specify DiscriminatorValue and nhibernate write some default value for each subclass in this column – Denis Borovnev Sep 05 '12 at 14:45