One thing you can do (per your application use-case
scenario) is to initialize those collections and
objects that you need using the
Hibernate.initialize(Object object) method.

For example, in my hibernate components, when I load
the object from the database, I will fetch those
collections up front:

Product product = (Product)
session.load(Product.class,
                                new Long(productId));

                Hibernate.initialize(product);
                //Hibernate.initialize(product.getCurrentPrice());
                Hibernate.initialize(product.getShipmentClass());
        
Hibernate.initialize(product.getShipmentClass().getShippingMethods());
                Hibernate.initialize(product.getAttributes());          
                Hibernate.initialize(product.getPrices());      

Hibernate.initialize(product.getProductReviews());      

-Phillip Rhodes
http://www.rhoderunner.com
http://www.jsso.org




                                        
                

--- Chris Chiappone <[EMAIL PROTECTED]> wrote:

> I have been developing an application using tapestry
> 3.0.3 and
> hibernate 3.0.2.  Took a while to get everything
> working correctly,
> and learning both of the frameworks.  I have a
> decent amount of the
> application written and now am running into 
> LazyInitializationException in certain places.  Its
> strange because I
> can't expain why I get the exception and other times
> I don't.
> 
> I've read that using the Spring Framework would fix
> this problem, but
> wonder if its worth learning another framework and
> trying to integrate
> it into my application.  I'm on a deadline and this
> may just take to
> long.  I've also extended BaseEngine to do this
> trying to relieve my
> problem:
> 
> 
>         protected void
> cleanupAfterRequest(IRequestCycle cycle) {
>                 try {
> 
>                        
> HibernateUtil.commitTransaction();
> 
>                         if (killSession) {
>                                 try {
>                                         HttpSession
> session =
> cycle.getRequestContext()
>                                                     
>    .getSession();
> 
>                                         if (session
> != null) {
>                                                
> log.info("Logging out user.");
>                                                
> session.invalidate();
>                                         }
>                                 } catch
> (IllegalStateException ex) {
>                                         // Ignore.
>                                 }
> 
>                         }
>                 } catch (NestedRuntimeException nre)
> {
> 
>                 } finally {
>                        
> HibernateUtil.closeSession();
>                 }
>         }
> Yet I still run into this exception:
> 
> Unable to resolve expression 'currentCompany.name'
> for
>
[EMAIL PROTECTED]
> binding: ExpressionBinding[EditProfile
> currentCompany.name] 
> location: context:/WEB-INF/EditProfile.page, line
> 41, column 65 
>    
> ognl.OgnlException 
> name 
>    
> org.hibernate.LazyInitializationException 
> could not initialize proxy - the owning Session was
> closed 
> messages: [Ljava.lang.String;@cdf872 
> throwableCount: 1 
> throwables: [Ljava.lang.Throwable;@7227a8 
> Stack Trace: 
>
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
>
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
>
org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
>
domain.company.Company$$EnhancerByCGLIB$$f029e618.getName(<generated>)
> 
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) 
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> java.lang.reflect.Method.invoke(Method.java:585) 
>  
> 
> Is there anything else I should try.  Or is the
> spring framework the
> way to go.  If so is it easy to integrate into an
> existing
> applicatoin?  Thanks.
> -- 
> ~chris
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to