Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate collection loading, "illegal access to loading collection"

I have a problem loading a collection. Setup I have is simple one-to-many association, mapped using FluentNHibernate. Entity is loaded withough exception being thrown, but accessing related collection displays "illegal access to loading collection". I'll paste relevant part of code here.

[Serializable]
[DataContract(IsReference = true)]
public partial class Department : Entity
{
    ...
    [DataMember]
    public virtual IList<PressJobRun> PressJobRun
    {
        get { return pressJobRunField; }
        protected set { pressJobRunField = value; }
    }
    ...
}

mapped as follows

public DepartmentMap()
{
    Id(x => x.Id);
    Map(x => x.Name)
        .Not.Nullable()
        .Length(100);
    HasMany(x => x.PressJobRun)
        .AsBag()
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .LazyLoad()
        .BatchSize(50);
    ...
}

I've also tried to disable lazy loading, by excluding the line and by calling .Not.LazyLoad(), however end is the same.

    using (var tx = m_Repository.Session.BeginTransaction())
    {
        var depts = m_Repository.Session.CreateCriteria<Department>().List<Department>();
        var dept = depts[0];
        ...
    }

I realize exposing Session is not the thing to do, but this was in attempts to make sure that session is open.

When I drill down the exception I see following stack trace:

   at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
   at NHibernate.Collection.AbstractPersistentCollection.Read()
   at NHibernate.Collection.AbstractPersistentCollection.ReadSize()
   at NHibernate.Collection.PersistentBag.get_Count()
   at NHibernate.DebugHelpers.CollectionProxy`1.get_Items()

The interesting gremlin comes here. I've set breakpoint on setter line of PressJobRun property.

  • if I step over it and quick watch pressJobRunField, I see the "illegal access to loading exception".
  • if I first quick watch value variable, I see the loaded collection. Stepping over setter line works as expected.

What I use

  • Visual Studio 2012, targeting .NET 4
  • NHibernate 3.3.1.400
  • SQL CE 4
  • Castle
  • Castle AutoTx facitlity
  • I manage session per WCF request myself

What I've tried

  • disabling lazy loading
  • made sure that session is open, and that it's same session during execution of offending code
  • toggled Inverted map of collection (thought I believe it should be inverted)
  • cleaned and rebuilt solution
  • made sure in Configuration Manager that all projects in solution are being built
  • set debugger to break on all thrown exceptions. Debugger doesn't break with exception that i see set in collection
like image 314
Nikola Radosavljević Avatar asked Aug 01 '12 11:08

Nikola Radosavljević


1 Answers

Problem is that when PressJobRub.Department property is set, my code also adds the PressJobRun to appropriate collection in Department. I did initialize the collection, but problem was that the collection which NHibernate uses fails for some reason when calling Contains() method. I'm still in wonder about gremlin I described in the question, and why the debugger didn't break on exception when I unticked 'Enable just my code debugging' and set it to break on Thrown exceptions too.

Anyhow, solution is to map PressJobRun.Department access to field (in my case to Access.PascalCaseField(Prefix.mUnderscore)) in order to avoid call that adds the entity to Department.PressJobRun collection.

Krzysztof Kozmic's article explained this, although in my case collection wasn't uninitialized.

like image 122
Nikola Radosavljević Avatar answered Oct 20 '22 14:10

Nikola Radosavljević