Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Problem with objects which are lazily initialized...

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a query. Let me tell you with an example. I have an employee and employer with moany to one relationship. So, i represented employer as a set in employee. and made that lazy initialized.
Now if i access only the employee object and closed the session.... i.e now i dont have the employer object in my employee object. Now after closing the session i tried to access employer within employee. That gives me an HibernateException. Can anyone tell me how to handle this??

thanks and regards,
Venkat Dasari
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

lazy collections (e.g. a List of Employee Objects) are only loaded when accessed. and they can only be loaded while the Session that loaded the owner of the collection (Employer, in your case i guess) is still open.
I see 3 solutions:
- leave the Session open until you are sure you do not need the collection anymore (e.g. in a webapp close the session in a filter)
- do not mark the collection as lazy (well not a real solution...)
- if you know you'll need the collection, then "touch" it while sesssion is open. this will trigger the "loading" of the collection. just call size() on the collection and your Objects will be there


cheers

pascal
 
Venkat dasari
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the information. My doubt is i dont know when the user will check the details for the employer. So, i cant open the session for long time. So, i hvae to either bring the employer object earlier(to mark the object as lazy) but that will be good for one or two tables. but what if i have a collection of tables in one mapping file. Neither i can put the entire thing in one session nor i can get the entire collection in one database call.
how to resolve this?
Thanks And Regards,
Venkat Dasari
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic