-->
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.  [ 10 posts ] 
Author Message
 Post subject: ICriteria.List returns CProxyType(..) in stead of expected
PostPosted: Mon Apr 02, 2007 7:49 am 
Newbie

Joined: Mon Apr 02, 2007 7:37 am
Posts: 1
When executing ICriteria.List, i get an accurate list back of object. There's one but: the first object in the list is not of the expected poco type (MyPocoClass), but something like CProxyTypeMyPocoClass_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2.

When executing the same criteria.list in a seperate unit test, all instances inside the collection are of the expected poco type (MyPocoClass).

I can't find the difference between the implementation that goes wrong and the unit test that runs as expected.

Anybody got a clue what could be the reason of this, or otherwise a way to prevent this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 12:19 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
A proxy class is something that NHibernate creates as part of 'lazy loading'.
I'd say that your class is lazy loaded but you need to load the full object (ie eagerly fetch) to be able to retrieve its type using (typeof(MyPocoClass).

Your other (separate) unit test probably has code which does fetch eagerly or overrides the 'lazy=true' setting somehow.

From here, I'd say hit the documentation or a book. It's not easy but knowing about this proxy and lazy loading is essential study material. Hopefully the words I've used are enough to get you the answers.


Top
 Profile  
 
 Post subject: I have the same problem. Did someone find a solution?
PostPosted: Wed Jul 04, 2007 3:41 am 
Newbie

Joined: Mon Jun 04, 2007 6:15 pm
Posts: 9
I have the same problem as ronaldvd.

Every object created by the lazy loading mechanism is of the correct type except 1 here and there. It seems to always be the same one.

Here is a snippet of the code that I use to Lazy load a collection in a persistant class:

Code:
       
public virtual void LazyInitCollection(System.Collections.IEnumerable Collection, Framework.Objects.FrameworkObject parent)
        {
                this.Session.Lock(parent, NHibernate.LockMode.None);
                //iterate over the collection in order for lazy loading to work
                foreach (Framework.Objects.FrameworkObject obj in Collection)
                {
                    obj.SetSession(this);
                    obj.SetRoot(parent.GetRoot());
                }
        }


Please if someone found a solution I would very much appreciate any tips or help.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 5:35 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi ronaldvd!

Probably you have requested this "bad" object before making ICriteria request - so it was already present in session cache in form of corresponding proxy - so when you made second request NHibernate return "existing instance" among newly created.

_________________
WBR, Igor


Top
 Profile  
 
 Post subject: Collection has items of type CProxyType if item was loaded
PostPosted: Wed Jul 04, 2007 6:24 am 
Newbie

Joined: Mon Jun 04, 2007 6:15 pm
Posts: 9
I figured out why the Proxytype objects is returned from my persistant collections.

It turns out that if you have another object with a link to the object in question, and the other object is loaded first: the item in the collection will be the CProxytype and not the entity itself.

In ohter words, if NHibernate session somehow already loaded the object before the object is loaded from lazyloading the collection: the item in the collection will be the CProxytype and not the entity itself.

I scrathed around in the Nhibernate source and in SessionImpl.cs there is a function: DoLoadByClass(System.Type clazz, object id, bool checkDeleted, bool allowProxyCreation)

the allowProxyCreation flag here might be a clue as to fix this problem.

Or alternatively, how do I get the actual entity associated with the first loaded proxy of the object? Ps, when I talk about the actual entity I talk about the instantiation of the class the proxy derives from associated with the proxy.

This is basically what IgorK said.

So how do we come past this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 4:04 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi tman!

What real problem do you have with proxy? Maybe it will be easier to overcome this problem than trying to "avoid" proxy in collection? Proxy must behave very similar to "non-proxy instance" - we only have some problems with XML serialization (just made custom serializer) and with reflection access (just add some code to take proxy into account - to transfer Type from proxy to it's BaseType).

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 7:23 am 
Newbie

Joined: Mon Jun 04, 2007 6:15 pm
Posts: 9
Hi IgorK

Thanks for replying and trying to help.

The software I developed does not like the CProxy object. It would be much harder to try and work around the problem than fix it at the source?

I guess what I was hopinig for is to be able to load the objects (the same instance if it was loaded before) without hassle.

I also use reflection quite a bit and I use events to propagate changes in my objects to all the GUI controls visualizing a particular object. This poses quite a problem when trying to make my code "proxy friendly".

Again thanks for helping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 11:10 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi tman!

Hmm... I think the only way to avoid proxy creation is to switch lazy loading off for all the classes - it may be really hard, as it may lead to enormous data loading (if your object graph is large enough and you use "normal" object associations - not "custom queries in getters")...

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 5:25 am 
Newbie

Joined: Tue Dec 27, 2005 5:02 am
Posts: 7
A less invasive workaround is just to close the current session and open a new session just before executing the Nhibernate statement that returns a CProxyType. This clears the first level cache and the correct type is returned.

Regards,

Ward


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 8:58 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
And if performance is a problem when you've cleared the session cache you can use the Second Level cache for both objects and queries.

Cheers,

Symon.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.