-->
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.  [ 9 posts ] 
Author Message
 Post subject: behavior of .load() with proxies?
PostPosted: Tue Dec 09, 2003 5:40 pm 
Newbie

Joined: Mon Sep 29, 2003 11:14 am
Posts: 6
I'm writing for more clarification on the expected behavior of using sess.load(class, id); We are using Hibernate 2.0

While going thru some junit testing, I noticed that certain objects would be returned as uninitialized proxies, and some others would be returned fully initalized.

I've been scouring the documentation to find information on this, and I've found a few posts by Gavin saying that using sess.load() can sometimes return un-initialized proxies. So my question is.. what factors are used to determine this?

I have two classes, A and B. Both classes (in their mapping files) declare an proxy (the value which is the class name of the class), and in my testing using sess.load(), A will be returned fully initialized, while B would be returned as an uninitalized proxy. The only difference between the two objects is that B has a collection, while A does not. Is this the reason? The code is identical for retrieving the objects other than the mapping file differences.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 5:57 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
How is the association A --- b mapped?

mota


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 5:57 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
How classes A and B are related? Could you paste mapping here?

I guess one of reasons of returning fully initialized proxy would be attempt to load already loaded (and initialized) object. I suppose what in the following code

Code:
Object o1 = session.load(Foo.class, new Long(100));
Hibernate.initialize(o1); // OR o1.getSomething();

Object o2 = session.load(Foo.class, new Long(100));


o2 will be fully initialized (moreover o2 == o1)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:12 pm 
Newbie

Joined: Mon Sep 29, 2003 11:14 am
Posts: 6
I can't post the mappings here - but here's a real quick example

Class eg.A, mapping is: proxy="eg.A"

Class eg.B, mapping is: proxy="eg.B"

There is no relationship between A and B. They are distinct objects with their own tables.

I've been continuing to dig around. I even tried to use Hibernate.initalize(foo); immediately after the session.load(), and sure enough, a query does show up for B (which did not previously) and but as soon as the object is returned, and I attempt to access any type of method on that object, it throws an CGLIB error "net.sf.hibernate.LazyInitializationException: Hibernate lazy instantiation problem: Could not initialize proxy - the owning Session was closed"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:16 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
How can B show up when you load A, if there is no relationship between A and B in the mappings?

mota


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:16 pm 
Newbie

Joined: Mon Sep 29, 2003 11:14 am
Posts: 6
Ok nevermind the last part. The Hibernate.initalize() does work, and it does init the object, but I was getting hte exception because I was attempting to print out the collection within object B, (which was lazy-loaded). So thats working as expected.

However, my question still stands. I'm still wondering why object A, which is also proxied, is returned to me as a fully instanced object. Is it because object A has no members that need to be lazy-loaded or anything. Is sess.load() supposed to ALWAYS return a proxy object, if one is declared in the mapping? Or is this a gray area?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:24 pm 
Newbie

Joined: Mon Sep 29, 2003 11:14 am
Posts: 6
Oh, I wasn't clear enough. I'm doing two separate calls.

example:

Code:
A objA = sess.load(eg.A, id)  // returns fully initalized object (A)
B objB = sess.load(eg.B, id)  // returns CGLIB proxy object (B)

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:24 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
load should return proxy if your mapping says proxy is used.

I would advise you to concentrate on class A - make very small test which opens session, loads A and closes session then and see what SQL is executed. IMHO there should be NO SQL at all.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:37 pm 
Newbie

Joined: Mon Sep 29, 2003 11:14 am
Posts: 6
Ok thanks all for your help. Found the problem - was a typo in the code for A.


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