[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-user
Subject:    Re: [Hibernate] Lazy again
From:       Joe Germuska <Joe () Germuska ! com>
Date:       2005-09-30 2:34:38
Message-ID: a0621020dbf6252466f58 () [192 ! 168 ! 1 ! 101]
[Download RAW message or body]

>Reading through the Hibernate manual it indicated that by specifying 
>fetch="join" the
>related table would be retrieved in the same select statement.  I 
>tried this but got the
>same "session is closed" result.  It also said that you could 
>specify lazy="false" and it
>would retrieve everything at once, however it immediately recommends 
>you don't do
>this for lots of good reasons (I did try some but they didn't seem 
>to work, maybe I put
>them in the wrong place).
>
>How does the filter modify this process for you?  It doesn't sound 
>like it has solved the
>problem.

The filter opens a single session as early as possible in the servlet 
request, and closes it as late as possible; this lets you write most 
of your code trusting that there is a current session which relates 
to recently retrieved objects.  Google for "Open Session In View" and 
you'll find lots and lots of writeups about this pattern.

Regarding Martin's question, the filter should be mapped to whatever 
your Struts actions are mapped (as in the conventional *.do) if you 
want your Struts actions and the views to which they forward to take 
advantage of the filter.

I've been using Hibernate for a little while and have adopted the 
Open Session in View pattern for the application I'm currently 
developing.  There are definitely a few things you need to get used 
to, so it is worth doing a decent amount of reading.  In fact, I 
would say that while you can do a good amount with Hibernate without 
much study, there is much careful thought behind the framework and 
you will use it much better if you learn the reasoning behind some of 
the implementation decisions. (Hibernate in Action, written by 
Hibernate founder Gavin King, is a very fine book; one of the most 
usefully theoretical technical books I've read in a long time.)

If you have a relatively simple object model, you can do just fine 
without "Open Session In View" -- just make sure that all your 
associations are marked as non-lazy or explicitly indicate the Fetch 
Mode in queries when you know that the associations will be followed, 
or use Hibernate.initialize(...) to make sure that the initialization 
happens (remember that it is not recursive, so you need to apply it 
to each lazy collection, and you must do it before the opening 
session has been closed, or you must use session.lock(...) to 
reattach your detached persistent objects before trying to traverse 
lazy associations.  (If this all sounds like gibberish, read 
Hibernate in Action!)

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic