• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why exception coming on accessing collection

 
Gaurav Verme
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate 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
Eclipse IDE Hibernate 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
Eclipse IDE Hibernate 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
Eclipse IDE Hibernate 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic