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.
pressJobRunField
, I see the "illegal access to loading exception".value
variable, I see the loaded collection. Stepping over setter line works as expected.What I use
What I've tried
Inverted
map of collection (thought I believe it should be inverted)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With