Hi,
I have following mapping for collection
IList<IOffice> Offices;
Code:
<bag name="Offices" table="Offices" inverse="true" lazy="true">
<key column="ParentId"/>
<many-to-many class="Office" column="Id"/>
</bag>
I try to read initialize proxy later. I call update for that:
Code:
public void InitProxy(object proxy)
{
using (ISession session = SessionFactory.OpenSession())
{
session.Update(proxy);
NHibernateUtil.Initialize(proxy);
}
}
On Update I get an exception:
Code:
System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[IOffice]' to type 'NHibernate.Proxy.INHibernateProxy'.
Server stack trace:
at NHibernate.Impl.SessionImpl.ReassociateIfUninitializedProxy(Object value)
at NHibernate.Impl.SessionImpl.Update(Object obj)
at Manager.InitProxy(Object proxy)
Looking through code for ReassociateIfUninitializedProxy I've found possible bug:
Code:
//NHibernateUtil
public static bool IsInitialized(object proxy)
{
if (proxy is INHibernateProxy)
{
return !NHibernateProxyHelper.GetLazyInitializer((INHibernateProxy) proxy).IsUninitialized;
}
if (proxy is IPersistentCollection)
{
return ((IPersistentCollection) proxy).WasInitialized;
}
return true;
}
//SessionImpl
internal bool ReassociateIfUninitializedProxy(object value)
{
if (!NHibernateUtil.IsInitialized(value))
{
INHibernateProxy proxy1 = (INHibernateProxy) value;
LazyInitializer initializer1 = NHibernateProxyHelper.GetLazyInitializer(proxy1);
this.ReassociateProxy(initializer1, proxy1);
return true;
}
return false;
}
//SessionImpl
public void Update(object obj)
{
this.CheckIsOpen();
if (obj == null)
{
throw new ArgumentNullException("obj", "attempted to update null");
}
if (!this.ReassociateIfUninitializedProxy(obj))
{
..skipped
}
..skipped
}
Please note IsInitialized is queried before explicit cast to INHibernateProxy. But !IsInitialized can return true even if proxy is IPersistentCollection.
Is this a bug or I've done something completely wrong?
Thanks,
Nikolay.