-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: LazyInitializationException
PostPosted: Thu Jul 28, 2005 4:06 am 
Newbie

Joined: Thu Jul 28, 2005 3:58 am
Posts: 7
Hibernate version:
3.0.5


I've recently upgraded to 3.0.5 from 2.1.8 and I'm running into LazyInitializationException on my web page that uses Struts...
It was working fine in 2.1.8, but I guess 3.0.5 now defaults the lazy initialization to be true which is causing a problem everytime it tries to render my jsp file being forwarded from the struts action.

So what are my choices to deal with this problem for Hibernate 3.0.5?

I researched the web, and some says use Spring framework, and others says to use Servlet Filters ....

I'm a bit lost as to what is the correct solution for dealing with this problem for Hibernate 3.0.5.

Also, if there is a way to change this default to false in a single place (maybe in the hibnerate configuration file?), please let me know as well. I have over 30+tables and to workaround by setting all of the columns to lazy=false would be lot of work ....

Please HELP!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 9:25 am 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
Quote:
Also, if there is a way to change this default to false in a single place (maybe in the hibnerate configuration file?), please let me know as well. I have over 30+tables and to workaround by setting all of the columns to lazy=false would be lot of work ....


Code:

      Configuration config = new Configuration().configure();

      for( Iterator collectionIterator = config.getCollectionMappings(); collectionIterator.hasNext(); ) {
         ((org.hibernate.mapping.Collection) collectionIterator.next()).setLazy( false );
      }

      SessionFactory sessionFactory = config.buildSessionFactory();


Top
 Profile  
 
 Post subject: Re: LazyInitializationException
PostPosted: Thu Jul 28, 2005 9:32 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
mappy wrote:
Hibernate version:
3.0.5


I've recently upgraded to 3.0.5 from 2.1.8 and I'm running into LazyInitializationException on my web page that uses Struts...
It was working fine in 2.1.8, but I guess 3.0.5 now defaults the lazy initialization to be true which is causing a problem everytime it tries to render my jsp file being forwarded from the struts action.

So what are my choices to deal with this problem for Hibernate 3.0.5?

I researched the web, and some says use Spring framework, and others says to use Servlet Filters ....

I'm a bit lost as to what is the correct solution for dealing with this problem for Hibernate 3.0.5.

Also, if there is a way to change this default to false in a single place (maybe in the hibnerate configuration file?), please let me know as well. I have over 30+tables and to workaround by setting all of the columns to lazy=false would be lot of work ....

Please HELP!!!!


If you're not already using Spring, I wouldn't recommend you add it just to deal with this issue.

You can change this behavior at the class-level of each mapping file
<class name="MyObj" table="MYTBL" lazy="true" >

But it's definitely not recommended because that could cause queries to be run and data to be retrieved that isn't required or necessary.

I would suggest you look at using a ServletFilter to close your session. Check out this link for the Open Session in View pattern http://www.hibernate.org/43.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 9:57 am 
Newbie

Joined: Fri Apr 01, 2005 5:21 am
Posts: 11
You can set all the collections to null after loading the object from the session, before closing the session. This will prevent Lazy Initialization Exception.

But if you do need certain collections, then also call Hibernate.initialize(whatevrObject.getWhateverCollection()).
This will load the collection. This should be done before closing the session.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 12:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
vibha131 wrote:
You can set all the collections to null after loading the object from the session, before closing the session. This will prevent Lazy Initialization Exception..


This is not good advice.


The solution is to have an explicit "assembly" phase, just like you would have if you were using EJB2+DTO, of to fetch all nedded data in the initial query using "left join fetch".


Top
 Profile  
 
 Post subject: Re: LazyInitializationException
PostPosted: Thu Jul 28, 2005 12:51 pm 
Newbie

Joined: Thu Jul 28, 2005 3:58 am
Posts: 7
First, thank all for responding. I have further questions about "Open Session in View patter". (Please bare with me as I am a newbie in this area... )

pksiv wrote:
mappy wrote:
Hibernate version:

I would suggest you look at using a ServletFilter to close your session. Check out this link for the Open Session in View pattern http://www.hibernate.org/43.html



I am starting to look into this, but can't figure out how to correctly fire the servletfilter.... I have specified the filter and filter-mapping in web.xml but doesn't seem to execut the doFilter code.

I copied HibernateFilter code and is correctly compiled into the classpath of the web server.

Here's what I did in web.xml

<filter>
<filter-name>hibernatefilter</filter-name>
<filter-class>HibernateFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>hibernatefilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>



..
09:43:43,968 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.adobe.automation.database.hibernate.TblProduct$$EnhancerByCGLIB$$d573865c.getTblProductConfiguration(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:314)
at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:920)
at org.apache.jsp.common_005fjsp.feathome_jsp._jspx_meth_c_forEach_0(org.apache.jsp.common_005fjsp.feathome_jsp:248)
at org.apache.jsp.common_005fjsp.feathome_jsp._jspService(org.apache.jsp.common_005fjsp.feathome_jsp:171)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
at javax.servlet.h


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 12:55 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You know, the filter has an init() method. The most trivial debugging would involve maybe a System.out.println("HEY I'M RUNNING") in that method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>my.package.HibernateFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 2:34 pm 
Newbie

Joined: Thu Jul 28, 2005 3:58 am
Posts: 7
christian wrote:
You know, the filter has an init() method. The most trivial debugging would involve maybe a System.out.println("HEY I'M RUNNING") in that method.


Thanks, this worked. I was using apache common log, and it wasn't somehow showing it in the console.

I just have one more question.

Where do I find HibernateUtil code? This is mentioned in the "Open Session in View" doc. There is one exist in the Hibernate Tutorial but this done doesn't contain the transaction management...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 2:35 pm 
Newbie

Joined: Thu Jul 28, 2005 3:58 am
Posts: 7
mappy wrote:
christian wrote:
You know, the filter has an init() method. The most trivial debugging would involve maybe a System.out.println("HEY I'M RUNNING") in that method.


Thanks, this worked. I was using apache common log, and it wasn't somehow showing it in the console.

I just have one more question.

Where do I find HibernateUtil code? This is mentioned in the "Open Session in View" doc. There is one exist in the Hibernate Tutorial but this done doesn't contain the transaction management...



Nevermind .... I found it from the link from "Open Session in View" page.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:10 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I would also recommend, if you're using Struts, to filter on the action Servlet and not on /*

This way, you are sure that the session is closed prior to returning to the Browser and also sure you don't close the session while forwarding or including a jsp.

Also, I found that with Struts at least, committing the transaction in the Filter was not helpful because if you received any kind of database exception you would no longer be able to display the message on the page where the exception occurred - say for a ConcurrentMod exception. Because by the time the Filter executes the commit, Struts has already forwarded you to another page.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:21 pm 
Newbie

Joined: Thu Jul 28, 2005 3:58 am
Posts: 7
pksiv wrote:
I would also recommend, if you're using Struts, to filter on the action Servlet and not on /*

This way, you are sure that the session is closed prior to returning to the Browser and also sure you don't close the session while forwarding or including a jsp.

Also, I found that with Struts at least, committing the transaction in the Filter was not helpful because if you received any kind of database exception you would no longer be able to display the message on the page where the exception occurred - say for a ConcurrentMod exception. Because by the time the Filter executes the commit, Struts has already forwarded you to another page.


I guess you are right .... This approach doesn't seem to work with Struts.
Well, I'll probably make everything non-lazy, or revert back to hibernate2 then....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:23 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
mappy wrote:
pksiv wrote:
I would also recommend, if you're using Struts, to filter on the action Servlet and not on /*

This way, you are sure that the session is closed prior to returning to the Browser and also sure you don't close the session while forwarding or including a jsp.

Also, I found that with Struts at least, committing the transaction in the Filter was not helpful because if you received any kind of database exception you would no longer be able to display the message on the page where the exception occurred - say for a ConcurrentMod exception. Because by the time the Filter executes the commit, Struts has already forwarded you to another page.


I guess you are right .... This approach doesn't seem to work with Struts.
Well, I'll probably make everything non-lazy, or revert back to hibernate2 then....


NO!

It works fine. I'm doing it in a very large application. Just filter on the action, not on the JSP's.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And Struts has no interceptor or no way to hook into the processing?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://raibledesigns.com/page/rd?anchor ... on_in_view


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.