-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: 1.2.0b3: Error while trying to reattach lazy initialized bag
PostPosted: Wed Jan 17, 2007 12:43 pm 
Newbie

Joined: Wed Jan 17, 2007 12:14 pm
Posts: 3
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 1:53 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Are you passing a collection to Update? Update expects to be passed an entity instance, not a collection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 8:19 am 
Newbie

Joined: Wed Jan 17, 2007 12:14 pm
Posts: 3
Thanks.

This was exactly the problem. Sorry.

Nikolay.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.