Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Object Relational Mapping
Problem with LazyInitializationException
Muhammad Ramahy
Greenhorn
Posts: 11
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I get this exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.itworx.twitter.model.User.messages, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:205) at org.hibernate.collection.PersistentBag.add(PersistentBag.java:297) at com.itworx.twitter.model.User.addMessage(User.java:74) at com.itworx.twitter.dao.UserDao.saveMessage(UserDao.java:77) at com.itworx.twitter.dao.UserDao$$FastClassByCGLIB$$b94a7438.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterc eptor.intercept(Cglib2AopProxy.java:621) at com.itworx.twitter.dao.UserDao$$EnhancerByCGLIB$$c39fc5d6.saveMessage(<generated>) at com.itworx.twitter.service.TwitterService.saveMessage(TwitterService.java:51) at com.itworx.twitter.controller.TileController.saveMessage(TileController.java:49) 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:597) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)
every time I try to execute this code:
public boolean saveMessage(Message message,User user) { session =sessionFactory.openSession(); try{ System.out.println("I'm in dao"); //the exception is fired here user.getMessages().add(message) user.setMessages(user.getMessages()); session.saveOrUpdate(user); //session.saveOrUpdate(message); }catch (HibernateException e) { e.printStackTrace(); System.out.println("Hibernate Exceptioun caught"); return false; }finally{ session.close(); } System.out.println("true is returned"); return true; }
I don't know what to do in order to avoid not having session or having multiple sessions and I wish Help
Regards,
Rishi Shehrawat
Ranch Hand
Posts: 218
I like...
posted 13 years ago
2
Number of slices to send:
Optional 'thank-you' note:
Send
The exception is caused due to detached object. You will have to use session.merge() or session.lock() to attach the object to the session.
http://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate
Muhammad Ramahy
Greenhorn
Posts: 11
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It works! thank you, I would also want to say that if I've added
fetch=FetchType.EAGER
to the oneToMany annotation it will also work (that's what I'm currently doing).
nivedan Nat
Greenhorn
Posts: 2
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
But adding FetchType.Eager defeats the purpose of Lazy Loading. Depends where your using this object, probably associate the entity into session so that lazy loading can happen.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
org.hibernate.MappingException : Unknown Entity?
How to create a new excel file on server
hibernate.AssertionFailure collection [] was not processed by flush()
Hibernate 3: java.sql.SQLException: Field 'passwordConfirmation' doesn't have a default value
Sql Exception: inconsistent datatypes, need help
More...