• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why exception coming on accessing collection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was going through the hibernate docs and came through the below code



Can you please explain that why its giving error at line 11 even if we have called the getPermissions() method on the retrieved object.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without knowing what error is being thrown all we can do is guess.
 
Gaurav Verme
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the permissions collection was not initialized when the Session was closed, the collection will not be able to load its state. Hibernate does not support lazy initialization for detached objects.
 
Gaurav Verme
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Any pointers why the above code will give exception
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no open session when you access your collection. The collection has not been initialized because (presumably) it is configured for lazy initialization (?) and there is no mechanism not to initialize it.
 
Gaurav Verme
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.

One thing need to confirm that once we are doing



then will it not fetch the collection from DB because we are calling the getter method on the same
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing how you've mapped this I can't say, but I'll guess your collection related to an association in the database? In which case Hibernate will try to load this if it is not already loaded. And it will fail if there is no open Session to use.
 
Gaurav Verme
Greenhorn
Posts: 10
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed it is. How is it related to your question? Are you using the second level cache? If you are then you understand that something has to put object into this cache in the first place, and nothing in your code does that.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic