| 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.
|
 |
 |
|
|
subject: Why exception coming on accessing collection
|
|
|