-->
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.  [ 5 posts ] 
Author Message
 Post subject: getting LazyInitializationException with open session
PostPosted: Fri Mar 16, 2007 11:55 am 
Beginner
Beginner

Joined: Wed Aug 09, 2006 11:26 am
Posts: 20
Hello everybody,

I'm using hibernate 3.1 in a web application with javaserver faces.
I use a thread local session and a servlet filter to close the session at the end of the request, so that I can access my objects from the jsp files.
I have the following mapping files:

<class name="User" table="User">
<set name="items" table="Item" >
<key column="userId"/>
<one-to-many class="Item"/>
</set>
...
</class>

<class name="Item" table="Item">
<many-to-one name="a" class="A">
<column name="aId" not-null="true"/>
</many-to-one>
...
</class>

<class name="A" table="A">
<many-to-one name="b" class="B">
<column name="bId" not-null="true"/>
</many-to-one>
...
</class>

<class name="B" table="B">
<property name="name" type="string">
<column name="name" length="128" not-null="true"/>
</property>
...
</class>

What I want to do in my jsp is:

<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:outputText value="#{item.a.b.name}" />
</h:column>
</h:datatable>

in my bean i have:
user = (User) session.get(User.class, id);
bean.getItems.addAll((user.getItems()))

and i get this exception:

javax.faces.el.EvaluationException: javax.faces.el.EvaluationException: Error getting property 'b' from bean of type mypack.domain.A$$EnhancerByCGLIB$$2e8df83: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed.

All my associations are lazy but since my session is not closed it should be possible to load a,b and title in my jsp.
What is wrong with this?

Thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 12:15 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I'd check your code again, because the session that loaded the object is closed.
1) make sure that it's the same session (should be, if you are using a thread local) and
2) make sure that the OSIV filter is the topmost filter (I'm not that familiar with JSF, but if it uses a filter, make sure it is chained inside of the OSIV filter) and lastly
3) don't use OSIV, hydrate your objects with as much info as you need for the view and then pass it to the UI layer

I used to use OSIV, but it's a pain to debug, breaks encapsulation/separation of concerns (IMHO, don't grief me on this!) and you need yet another filter to trap any exceptions that occur in the lazy loading of the view! Ick! If it looks like an anti-pattern, smells like and anti-pattern and feels like an anti-pattern... ;D


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 1:20 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ananasi wrote:
I used to use OSIV, but it's a pain to debug, breaks encapsulation/separation of concerns (IMHO, don't grief me on this!) and you need yet another filter to trap any exceptions that occur in the lazy loading of the view! Ick! If it looks like an anti-pattern, smells like and anti-pattern and feels like an anti-pattern... ;D


It's a pattern that is used in almost all Hibernate applications with great success. You do _not_ catch LazyInitializationException. That is the whole point of the pattern. There is ready to use code linked here: http://hibernate.org/42.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 18, 2007 12:26 pm 
Beginner
Beginner

Joined: Wed Aug 09, 2006 11:26 am
Posts: 20
Thank you for reading my post and your replies,

so from your answers and having read the documents on the site I get that my code should be working. For some reason though my session is closed during the rendering of the view. I will look into it.

Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 19, 2007 9:23 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
christian wrote:
It's a pattern that is used in almost all Hibernate applications with great success. You do _not_ catch LazyInitializationException. That is the whole point of the pattern. There is ready to use code linked here: http://hibernate.org/42.html

Don't worry brother, I was just poking fun, that's what my winky emoticon was for :D. My UI jockeys wouldn't know an N+1 selects problem if it bit them in the rear. If I don't completely fill out relevant objects graphs for them my database server would be hella pissed!

I find that OSIV is fine if your backend and UI programmers are one and the same person, but on more complex projects where concerns are highly delineated, you just end up forcing your UI developers to learn complex SQL concepts. My current project is 380 ridiculously overengineered tables (the DBAs had two years to make as much of a mess as possible), and there's no way I'd let the UI guys navigate a lazy-loaded domain graph.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.