Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Spring and the fly likes OpenSessionInViewFilter does not help, Spring + Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "OpenSessionInViewFilter does not help, Spring + Hibernate" Watch "OpenSessionInViewFilter does not help, Spring + Hibernate" New topic
Author

OpenSessionInViewFilter does not help, Spring + Hibernate

Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Hello all,


I am using spring 2.5 and hibernate 3.2 version and a view framework ZK.

My problem is associated with Lazy association fetching. I am using OpenSessionInViewFilter and configured in web.xml successfully.
I can see it working with the spring DEBUG and I see the following out put when a request is initiated :





However, I am unable to get the Lazy loading working. I get the following exception when tried to fetch a Lazy association :



The exception stack trace also shows these lines at the start :


Which shows that OpenSessionInViewFilter did get called and opened the session.

For my own debugging purpose I wrote this line of code just before the line which tries to lazy load the data :


And checked to see if SessionHolder is null, but to my surprise I did get a full object and that means that there is a session bound to thread, then why am I getting the above exception.

I have also got the spring DEBUG logs which shows there is a session bound to the thread:




What am I doing wrong here? I simply can't get Lazy load to work, and right now I am left with nothing but to turn off Lazy loading.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's impossible to help with the information provided--the OSiV filter works, people use it all the time. It's possible there's something in the framework that renders it non-functional--have you tried doing something in a simple servlet to make sure it's working at least that far? If it is, you'd then need to start looking at how the framework handles requests.You know about import statements, right?
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Hi David,

Thanks for the reply.



You know about import statements, right?



Actually, that code is from .zul file where I am rendering my entity (User's information) to be updated. And .zul file requires you to mention fully qualified name of the class.

"Hibernate does not support Lazy initialization for detached object"

David, could this be the reason?

If I understood this statement correctly, then is it required that you have to lazy initialize an association in the same hibernate session?

Hibernate doc says :




Now, what, if I do this



In this case there is a session, the error mustn't appear now. Please help me get clarified on this issue, please.






David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the object isn't attached to any session, then the error message is correct--you'd need to associate it with the current session before lazy loading would work.
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Thanks again David for your prompt and quick reply.

So, I need to associate my detached User object with the current session and then only I will be able to fetch the association.

I am using Spring's hibernate template, which method of hibernate template I must use to bind my detached instance to current session?

Sorry, a newbie to spring and hibernate is asking you the question.

:-)

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No apologies necessary :) Particularly since I don't remember off the top of my head ;) I think there's a merge() method?
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 199
Thank you David, it did the trick.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm always amazed when I remember something ;) Glad you got it working!
 
Consider Paul's rocket mass heater.
 
subject: OpenSessionInViewFilter does not help, Spring + Hibernate
 
Similar Threads
Exception on Save operation with Spring annotation
entity was not persistent in delete processing
Lazy Initialization Exception while running my Junit Test Cases
How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?
LazyInitializationException [spring + struts2]