Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OpenSessionInViewFilter does not help, Spring + Hibernate

 
Girish Vasmatkar
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you David, it did the trick.

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm always amazed when I remember something ;) Glad you got it working!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic