-->
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.  [ 13 posts ] 
Author Message
 Post subject: Calling instance method on a proxy does not instantiate
PostPosted: Thu Nov 17, 2005 7:16 pm 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
Hi.

I have a simple object that is cached, the mapping is as follow:

<class
name="Assessment"
table="assessment"
lazy="true"
>
...

Calling session.load() on the Assessment object the "2nd time" returns me a Assessment$$EnhancerByCGLIB$$ object, this is expected since hibernate returns me a proxy object.

The problem is, now if I call any instance method on the proxy object it does not instantiate my Assessment object. My design requires me to use lazy="true".

What should I do to make my Assessment object instantiate from a proxy?

Any help is great appreciated. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 8:25 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Hibernate.initialize()

or what exactly are you trying to do?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 9:40 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
This is exactly what I am trying to do:

1. Open session in java action
2. Session.load() object
3. Close session in java action
4. Set object in request attribute
5. Forward control to jsp
6. Access the object in the JSP

I tired using HQL query to return the object, it allows me to access it in the JSP after I call Hibernate.initialize(). But if I use session.load, even after Hibernate.initialize(), it would not work.

To clarify:

*this does not work
1. Open session in java action
2. Session.load() object
3. Hibernate.initialize() on object
3. Close session in java action
4. Set object in request attribute
5. Forward control to jsp
6. Access the object in the JSP

*this works
1. Open session in java action
2. Use Hql to load in object
3. Hibernate.initialize() on object
3. Close session in java action
4. Set object in request attribute
5. Forward control to jsp
6. Access the object in the JSP


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 9:46 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Load returns proxies, hql list doesn't.

What do you mean by it doesn't work?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 9:57 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
Quote:
Load returns proxies, hql list doesn't.

Is this really the case? Both returns me a $$EnhancerByCGLIB$$ Object.

Quote:
What do you mean by it doesn't work?

I apologize for the confusion. It doesn't work meaning in the java action, when I call Assessment.getName(), it returns me some string. In the jsp, calling Assessment.getName() returns me null.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 10:04 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Quote:
1. Open session in java action
2. Session.load() object
3. Close session in java action
4. Set object in request attribute
5. Forward control to jsp
6. Access the object in the JSP


Quote:
when I call Assessment.getName(), it returns me some string. In the jsp, calling Assessment.getName() returns me null.


It must throw LazyInitializationException, are you sure it return null ?[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 10:17 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Just looked through the code.
Yep, Query.list() also returns proxies.

And the second one ... like baliukas said ... it should throw LazyIException, not null.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 10:38 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
It is actually throwing a nullPointerException but that may be because of my custom tag in the JSP.

Now, assuming it throws LazyIException, I suppose the main question is, why would it throw a LazyIException even if I called Hibernate.initialize() after a session.load()?

And why does this not happen if I use HQL then call Hibernate.initialize()?

Again, any help is appreciated. I've started refactoring my services using HQLs... :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 10:51 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
"Hibernate.initialize() " works without problems and it is covered by unit test, probably your problem is not related to "initialize". Do you have a stack trace ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 11:06 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
Yes I belive the problem is not in the initialize(). I will dwell into my code deeper before another post. Thanks for all your help! Greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 11:19 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
Just another quick question : p. Is there any easy way to convert com.appiancorp.solutions.sox.framework.Account$$EnhancerByCGLIB$$ into just a Account object?

The only way I can think of is declaring a new object and copy the properties of Account one by one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 11:36 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, you can cast it to HibernateProxy and get real object (LazyInitializer.getImplementation())


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 11:46 am 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 10
I'll give that a try right away. You're a genius baliukas.


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