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.  [ 2 posts ] 
Author Message
 Post subject: LazyInitializationException when trying to serialize class?
PostPosted: Wed Jul 23, 2008 7:09 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Hibernate 3.2, Java 1.5, MS-Windows XP

I am currently in the process of transitioning a project from Hibernate 2 to Hibernate 3.2 and I am running into a few problems. The current issue is that I have a class 'User.java' which is being lazy loaded and then at some point it is meant to be serialized to be sent over a network connection. At the point of serialization I get the following exception:

Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
   at com.myco.entity.general.Groupe$$EnhancerByCGLIB$$b2e7bf5a.toString(<generated>)
   at com.myco.entity.tools.Log4jToolkit.beanDataToString(Log4jToolkit.java:57)
   at com.myco.entity.general.Usager.toString(Usager.java:180)
   at com.myco.businessLogic.BasicServicesImpl.authenticationUser(BasicServicesImpl.java:1627)
   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 electric.util.reflect.Invocation.execute(Unknown Source)
   at electric.util.reflect.Invocation.invoke(Unknown Source)
   at electric.service.object.ObjectService.invoke(Unknown Source)
   at electric.soap.local.handlers.service.SOAPToServiceHandler.invoke(Unknown Source)
   at electric.soap.local.handlers.service.SOAPToServiceHandler.handle(Unknown Source)
   at electric.soap.security.handlers.SecurityHandler.handle(Unknown Source)
   at electric.soap.handlers.interceptor.SOAPInterceptorHandler.handle(Unknown Source)
   at electric.soap.routing.RoutingHandler.handle(Unknown Source)
   at electric.soap.handlers.logging.SOAPLoggingHandler.handle(Unknown Source)
   at electric.soap.handlers.setup.SetupHandler.handle(Unknown Source)
   at electric.soap.http.handler.HTTPToSOAP.service(Unknown Source)
   at electric.server.http.ServletServer.service(Unknown Source)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at electric.servlet.Config.service(Unknown Source)
   at electric.servlet.HTTPContext.service(Unknown Source)
   at electric.servlet.ServletEngine.service(Unknown Source)
   at electric.webserver.WebServer.service(Unknown Source)
   at electric.net.socket.SocketServer.run(Unknown Source)
   at electric.net.socket.SocketRequest.run(Unknown Source)
   at electric.util.thread.ThreadPool.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:595)


If I understand the issue it is that certain attributes of the 'User' class are replaced with proxy objects and these fail to serialize because we are now out of session. What is the best way around this issue?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:48 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Bascially, you have an entity that has properties loaded lazily. But, you care closing the session, and when the application goes to grab the lazily loaded data, there is no connection open to the database over which to do it.

I discuss the LazyLoadingException here in this
Hibernate tutorial. You might find it interesting:

Understanding How Hibernate Works

How do you avoid the LazyLoadingInitializationException? Use longer sessions. Open the Session when a request comes in, and close it when it leaves, using a ServletFilter or something. It's known as the Open Session in View Pattern, and it's a standard.

Here's more on
Hibernate transactions that you might find interesting. Query Open Session in View as well, you'll get some good stuff.

http://www.hibernate.org/42.html

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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