aspose file tools
The moose likes Object Relational Mapping and the fly likes Why exception coming on accessing collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Why exception coming on accessing collection" Watch "Why exception coming on accessing collection" New topic
Author

Why exception coming on accessing collection

Gaurav Verme
Greenhorn

Joined: Sep 15, 2010
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

Without knowing what error is being thrown all we can do is guess.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gaurav Verme
Greenhorn

Joined: Sep 15, 2010
Posts: 10
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

Joined: Sep 15, 2010
Posts: 10
Hi,

Any pointers why the above code will give exception
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Sep 15, 2010
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Sep 15, 2010
Posts: 10
This is from hibernate documentation:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-cache
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why exception coming on accessing collection
 
Similar Threads
[hibernate] How to load lazy collections
response.redirect and jsp:forward
Hibernate's update problem
Access denied for user
OpenSessionInViewFilter does not help, Spring + Hibernate