You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Martin Ravell <st...@rave-tech.com.au> on 2005/09/30 02:30:39 UTC

Lazy again

I'm just getting back to looking at my lazy loading problem and would like
to see if I am understanding things correctly. While not strictly Struts
related (I do use Struts in this app) I am hoping that someone else on this
list has gone down the same path as I am attempting.

My web.xml has the following section in it:

<filter>
  <filter-name>hibernateFilter</filter-name>
 
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFi
lter</filter-class> 
</filter>

<filter-mapping>
  <filter-name>hibernateFilter</filter-name>
  <url-pattern>*.spring</url-pattern>
</filter-mapping>


I have also tried other patterns than *.spring (as I was unsure if this was
correct.

When attempting to access objects in the jsp that have come from hibernate I
am still getting :

[LazyInitializationException] could not initialize prozy - the owning
Session was closed................

Originally I was using Hibernate 2 and had tried the similar
OpenSessionInViewFilter that is meant for that version. Same problem.

Any ideas?

Am I correct in thinking that if the filter is working correctly I should be
able to grab an object from my DAO layer in an Action, put it into the
session or request scope and have my jsp page access it's related objects
(with lazy loading)?

What mechanism opens and closes these Hibernate sessions? I thought it was
supposed to be all done by the filter?

PS. I do use Tiles with my Struts if this makes any difference.



Regards
Marty




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


Re: [Hibernate] Lazy again

Posted by Joe Germuska <Jo...@Germuska.com>.
>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


Re: [Hibernate] Lazy again

Posted by Murray Collingwood <mu...@focus-computing.com.au>.
Hi Martin

I'm not using spring, not sure if I should be.  I recently tried writing an app using 
Hibernate and had some great experiences - it's pretty cool in the amount of code and 
development time it saves me.

Having said that I'm struggling with the same problem.  I open the Hibernate session in 
my Action class, update / access the data and then close the session.  I get the "session 
is closed" when the page begins to render properties from my bean (where those 
properties originated from related tables).

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.

Kind regards
mc


On 30 Sep 2005 at 10:30, Martin Ravell wrote:

> I'm just getting back to looking at my lazy loading problem and would like
> to see if I am understanding things correctly. While not strictly Struts
> related (I do use Struts in this app) I am hoping that someone else on this
> list has gone down the same path as I am attempting.
> 
> My web.xml has the following section in it:
> 
> <filter>
>   <filter-name>hibernateFilter</filter-name>
>  
> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFi
> lter</filter-class> 
> </filter>
> 
> <filter-mapping>
>   <filter-name>hibernateFilter</filter-name>
>   <url-pattern>*.spring</url-pattern>
> </filter-mapping>
> 
> 
> I have also tried other patterns than *.spring (as I was unsure if this was
> correct.
> 
> When attempting to access objects in the jsp that have come from hibernate I
> am still getting :
> 
> [LazyInitializationException] could not initialize prozy - the owning
> Session was closed................
> 
> Originally I was using Hibernate 2 and had tried the similar
> OpenSessionInViewFilter that is meant for that version. Same problem.
> 
> Any ideas?
> 
> Am I correct in thinking that if the filter is working correctly I should be
> able to grab an object from my DAO layer in an Action, put it into the
> session or request scope and have my jsp page access it's related objects
> (with lazy loading)?
> 
> What mechanism opens and closes these Hibernate sessions? I thought it was
> supposed to be all done by the filter?
> 
> PS. I do use Tiles with my Struts if this makes any difference.
> 
> 
> 
> Regards
> Marty
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> -- 
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.344 / Virus Database: 267.11.9/115 - Release Date: 29/09/2005
> 



FOCUS Computing
Mob: 0415 24 26 24
murray@focus-computing.com.au
http://www.focus-computing.com.au



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.9/115 - Release Date: 29/09/2005


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