-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: LazyInitializationException: failed to lazily initialize...
PostPosted: Thu Apr 28, 2005 12:46 am 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
Hibernate version: 3

I'm trying to understand this problem. Basically, I have an Account. Accounts can have privileges (which is a set).

Everything works perfect if I have it in one page; for instance: if I do:

1) Retrieve account

2) display account.getPrivileges

everything works fine.

The problem arises when I tried keeping my account in a session (after logging in) and then trying to access it's privileges - that's when I get this exception:

LazyInitializationException: failed to lazily initialize a collection

Here's what I invision happens:

1) On login, we retrieve the account. We're using session #1.

2) After retrieving and verifying it, we store it in a session attribute.

3) When we send response, our filter closes session #1.

4) The user clicks onto some other page. Now he's in a different thread, and therefore session #2.

5) We get the account out of the session attribute (which was associated with session #1)

6) We try to access the collection with session #2, and this for some reason gives us problems.


Am I getting this right? If you session.get() an object with one session, and then close that session, you cannot retrieve whatever bags or collections that object had?

If so, what is the solution? The only thing I can think of is to store the ID in the session instead of the object itself, and then retrieve the object when we get to the new page... But this is pretty inefficient.

Please advise.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException: failed to lazily initialize
PostPosted: Thu Apr 28, 2005 3:56 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
ivj wrote:
Am I getting this right? If you session.get() an object with one session, and then close that session, you cannot retrieve whatever bags or collections that object had?

If so, what is the solution? The only thing I can think of is to store the ID in the session instead of the object itself, and then retrieve the object when we get to the new page... But this is pretty inefficient.

Please advise.


All similar stuff has been answered in 'Hibernate in Action' book.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:29 pm 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
I don't know if I'm that commited to Hibernate yet to buy a $40 book, considering that there are lots of other similar ORM frameworks out there.

I mean this is a VERY BASIC simple question, and the response that I get in this "high traffic" forum is "buy a book"? What exactly is the forum for, then?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The forum is for people who have made a decent, honest effort to find information themselves.

The forum rules are here:

http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:41 pm 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
You say I didn't make an honest effort?

I could've, like most people here, post the error that I'm having as soon as I got it, and just dumped all my code here.

Instead, I spend hours working through the code, trying different scenarios, FINDING A SOLUTION myself, and now I'm simply asking IS THIS THE BEST SOLUTION OR NOT.

And instead of arguing, you could've taken that time to answer my question, which for you (being a "Hibernate developer") should be a 10-second task.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Some kinds of issues (application architecture and such) cannot be explained in forum comment boxes. You need an extended discussion. That's (I guess) why alesj recommended the book, which spends a lot of time dealing with these issues. He was being helpful, and you bit his head off for absolutely no reason. Then when politely I pointed you to the forum rules, you try to argue with me as well. If you continue in this vein, we'll have no choice but to ban you. I'm sorry about that, but we have enough stress dealing with tens of thousands of Hibernate users, without wasting time on stuff like this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 5:05 pm 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
Oh great so I'm the bad guy now.

You know, I'm just not going to talk back.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 8:52 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
I'll try to answer - my guess is that you'd want to treat the object as any object disconnected from [Hibernate] session, you'd need to first reassociate the object with the new Hibernate session, then try to call any collection accessors on the object. Updating and manipulating detached objects is covered in the free documentation: here

Option #2 is to make the collection not lazy, but you probably guessed that.

IMHO your question was good, and not nearly as banal as many of the questions that get asked here. Maybe gavin was just having a bad day :P


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 9:40 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Btw, this is all now fully documented, for free, in the Hibernate 3.0 reference documentation. Stop answering people who haven't read any docs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 10:11 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
christian wrote:
Btw, this is all now fully documented, for free, in the Hibernate 3.0 reference documentation. Stop answering people who haven't read any docs.


Off-topic, but why discourage users from helping users?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, I don't think we should discourage people from helping other people, but we also want to encourage people to learn to help themselves. Give a man a fish...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 10:55 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
gavin wrote:
Well, I don't think we should discourage people from helping other people, but we also want to encourage people to learn to help themselves. Give a man a fish...


So an appropriate answer to a poor question might be "here's the answer, and here's where you should have found it (as specific as possible)". Agreed.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Which is exactly what this document does: http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 8:08 pm 
Newbie

Joined: Thu Jan 13, 2005 3:46 pm
Posts: 3
Hey Gavin,
I was just browsing the forum, and I just read this post. First of all, I wanted to thank you, christian, and the others for investing so much time in this forum. You guys share a lot of valuable knowledge for free, and you've helped me and thousands of developers.
After reading this post, I felt that you guys were a little harsh on ivj. PLEASE DON'T BAN ME!!! I LOVE YOU GUYS!!!
I mean, he asked a nice enough question, and the response he got was "buy the book". If that was you, wouldn't you be a little teed of? PLEASE DON'T BAN ME!!! I LOVE YOU GUYS!!! I BOUGHT THE BOOK!!!
I myself have posted questions and gotten the "if you have searched the forum, you'd find the answer" response. Your forum's search engine isn't terribly sophisticated, and I haven't figured out how to do phrase searches. I perform a search, and I'll get a thousand links back. PLEASE DON'T BAN ME!!! I LOVE YOU GUYS!!! I'D GO THOUGH TEN THOUSAND RESULTS IF I HAVE TO!!!
Couldn't you give us the benefit of the doubt that we've already spent a good enough time before posting the question? I could give you the most sophisticated question, but I'm sure I would be "wasting" someone's time because there already are books, faqs, guides, and millions of forum posts. I just don't know what constitutes a stupid question without getting the "HowToAskForHelp" url sprung on me. PLEASE DON'T BAN ME!!! I LOVE YOU GUYS!!! I READ THE HOWTOASKFORHELP PAGE, TWICE!!!
Ivj is one of many developers holding off the $40 book to see if the framework is for them. I'd rather you don't intimidate new users because the more popular Hibernate gets, the more valuable my Hibernate skills are to the industry. PLEASE DON'T BAN ME!!! I LOVE YOU GUYS!!! I ACTUALLY WORK FOR FREE!!!
Maybe we should start each post with "I have read all of your faqs, guides, and forum posts, but I have a problem with…"

- Devout, Most Pious Hibernate Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 8:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It's not that we are deliberately trying to be mean, just that we try to train people to make use of existing resources, because we have no chance on earth to answer all questions from, for example, the hundreds of thousands of people who downloaded Hibernate3 in the past two months.

Note that the original recommendation to check HiA came, not from us, but from another user (who I have never met) who was trying to be helpful. And then the original poster bitched at him/her. Now, one of the rules of the forum is that the people who answer questions are allowed to yell at other people from time to time, but the people who only ask questions are not. I think that's a fair rule, and the original poster broke it, hence the threat of banning.

Note that we have never, ever threatened to ban someone just for asking questions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.