File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Problem with LazyInitializationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with LazyInitializationException" Watch "Problem with LazyInitializationException" New topic

Problem with LazyInitializationException

Muhammad Ramahy

Joined: Jun 14, 2011
Posts: 11

I get this exception:

every time I try to execute this code:

I don't know what to do in order to avoid not having session or having multiple sessions and I wish Help

Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

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.
Muhammad Ramahy

Joined: Jun 14, 2011
Posts: 11

It works! thank you, I would also want to say that if I've added to the oneToMany annotation it will also work (that's what I'm currently doing).
nivedan Nat

Joined: Jul 22, 2011
Posts: 2

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.
I agree. Here's the link:
subject: Problem with LazyInitializationException
It's not a secret anymore!