-->
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.  [ 4 posts ] 
Author Message
 Post subject: Trouble with detached object and LazyInitializationException
PostPosted: Thu May 11, 2006 2:23 pm 
Newbie

Joined: Wed Apr 05, 2006 5:58 pm
Posts: 13
Hibernate version: 3.1


I'm working on a web app built with in a basic MVC pattern. I had planned to use a session per request strategy with hibernate, however, i'm having some trouble. I've been reading up on session per conversation, but I don't see how it will address my issue. So, here it is:

I'm writting a multi-page registration process. In the case where a new user is registering, when they submit the first page, i get a bunch of information that I set into my user object. The user object contains a Set of addresses. When I attempt to check if that set contains any addresses, I get a LazyInitializationException. Since my User object does not refer to anything in the database, nor should it, I'm not sure how to proceed. Certainly catching and ignoring the exception isn't appropriate and none of the methods on session (merge, update, save, etc) seem appropriate.

Also, how does one reconnect to a session? Should I be using ThreadLocalSessionContext.bind()/unbind() for this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 3:09 pm 
Newbie

Joined: Tue Mar 14, 2006 6:48 pm
Posts: 9
To reconnect an object to a session do there are 2 choices:

1. Use Session.update
Code:
    session.update(userObject);


2. Use Session.lock
Code:
    session.lock(userObject, org.hibernate.LockMode.NONE);


The docs talk about the dettached state of an object (dettached from the Hibernate Session).

http://www.hibernate.org/hib_docs/v3/re ... ml_single/
(10.6. Modifying detached objects)

I hope that helps.

Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 4:00 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
In order for lazy loading to work, you have to have the object attached to a session, and the lazy loading must occur inside a transaction.

Either of RockStyle's suggestions will reattach the object to a session.

Then all you have to do is:

Code:
session.beginTransaction();
// Do stuff that requires lazy loading
session.getTransaction().commit();


If the lazy loading occurs in a JSP, you have a few options. You can use scriptlets to reattach the object to the session and surround the lazy loading with a transaction, or you can build some custom tags so that you don't need to use scriptlets. Depending on your setup there may be other solutions, but those are the most straightforward (not necessarily the most elegant or painless solutions, but the easiest ones to test that you can get lazy loading to work).


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 12:59 pm 
Newbie

Joined: Wed Apr 05, 2006 5:58 pm
Posts: 13
I bailed on this stuff and got a session per conversation pattern going. The key was getting the ExtendedThreadLocalSessionContext working from the Caveat Emptor example


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

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.