• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

failed to lazily initialize

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic