File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes failed to lazily initialize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "failed to lazily initialize " Watch "failed to lazily initialize " New topic
Author

failed to lazily initialize

Dupon David
Greenhorn

Joined: Aug 11, 2006
Posts: 19
failed to lazily initialize
hello
here my servlet:
when I run the server, I obtain the following error:

knowing that my session bean:


Thank you
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Here is my reply I have already given you, when you posted this same question in the EJB forum, and I moved it here for you.

Why did you delete that thread that had my response?

Exactly. In your Facade is where you get your EntityManager, this is the same as a Session in Hibernate. So you do a query with the EntityManager in the Facade, right now, Hibernate will lazy load your result set, maining there are prozy objects in your List, they only have the IDs, so when you return the List back to the Servlet, you are no longer inside a "transaction/session" with the EntityManager, it is gone. Then you try to access these "proxy" objects that do not have the full information, but it can't get that data because you no longer have the EntityManager.

Off the top of my head, I don't remember the exact syntax to make it eager fetch, since it looks like only one table, but I do know that in your Facade, after you get the ResultList, if you try to access the Collection, it will load the data for you, then you can return it from the Facade.

Your Servlet cannot access the database to initialize your data because you are not in a Hibernate Session.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I think the quick and dirty way is to call size() on the list before you return it. But you must also consider if the items in the list are intialized too. Maybe you need to hold open your session or some different session management.

The clean way is to call Hibernate.initialize() on the list before you return it. This works on persistent objects so I hope it works on lists from queries as well.
[ December 29, 2006: Message edited by: Mr. C Lamont Gilbert ]
K Karthik
Greenhorn

Joined: Jul 05, 2007
Posts: 11
Sorry for replying to a very old post. I had the exact same issue while using the @ManyToOne mapping in my application. I blindly followed your "quick and dirty" way and it worked just like that. Now, I am eager to know as how it worked and what's the issue previously. I was not able to comprehend the reason from the above posts.
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Suppose you have a One to many relation between A and B. When you fetch an instance of A you will be getting a Collection of object B those related to A. Now you might not be needing all the Entities in that collection and hence the persistence context lazily loads the objects, That is it will have a collection proxies of object B not containing any data except the primary key field of B. Now only when you access the collection your Entity manager will load the actual data for you.

By default i believe in JPA it fetches the collection eagarly. Have you specified it anywhere in you application to fetch type as Lazy?
K Karthik
Greenhorn

Joined: Jul 05, 2007
Posts: 11
I understand a part of what you said. So, in short by calling the size() method you are actually "waking it" up? I haven't mentioned Lazy fetch anywhere. Infact I have a many-to-many mapping between two of my entities which does the eager fetch properly. I am not sure why this is not getting following the eager fetch. I will look into it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: failed to lazily initialize
 
Similar Threads
"Mapping RelationShip State" in chapter 8
@ManyToOne and @OneToMany : 2 primary keys
@JoinTable or @JoinCol...
Repeated column in mapping for entity
Struts 2 checkboxlist error - list key could not be resolved